Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
<html>
<head>
<title>Create Table Example (VBA)</title>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="ApiHelp.css">
<meta name=MS-HKWD content="VBA examples">
<meta name=MS-HKWD content="VBA examples, Tables">
<meta name=MS-HKWD content="VBA examples, TableStyles">
<meta name=MS-HKWD content="VBA examples, TableStyle Manager">
<meta name=MS-HKWD content="VBA examples, Layers">
<meta name=MS-HKWD content="VBA examples, transparency">
<meta name=MS-HKWD content="VBA examples, Colors">
</head>
<body>
<h1>Create Table Example (VBA)</h1>
<p>This example shows how to change the transparency of the active Layer, create
a TableStyle and Table, and change the background colors of the top row and a cell
in the Table.</p>
<pre>'-------------------------------------------------------------
' Preconditions:
' 1. Create a VBA macro in a software product in which VBA is
' embedded.
' 2. Copy and paste this example into the Visual Basic IDE.
' 3. Add a reference to the DraftSight type library,
' <i>install_dir</i><b>\bin\dsAutomation.dll.</b>
' 4. Open the Immediate window.
' 5. Start DraftSight and open a new document.
' 6. Run the macro.
'
' Postconditions:
' 1. Gets the transparency object for the active Layer and prints its
' transparency percentage to the Immediate window.
' 2. Changes the transparency percentage of the active Layer and prints
' its new percentage to the Immediate window.
' 3. Creates and activates a new TableStyle named <b>Sample Table Style</b>.
' 4. Inserts a Table and applies the <b>Sample Table Style</b> TableStyle.
' 5. Gets the transparency object for the Table.
' 6. Prints whether the transparency object for the Table is <b>ByLayer</b>.
' 7. If not, then changes and updates the transparency object
' of the Table to <b>ByLayer</b>.
' 8. Changes the top row's background color.
' 9. Changes the background color of the cell in the second row
' and second column to match the top row's background color.
'10. Prints the name of the TableStyle to the Immediate window.
'11. Examine the drawing and the Immediate window.
'----------------------------------------------------------------</pre>
<pre>Option Explicit</pre>
<pre> Sub main() </pre>
<pre> Dim dsApp As DraftSight.Application </pre>
<pre> 'Connect to DraftSight application
Set dsApp = GetObject(, "DraftSight.Application") </pre>
<pre> 'Abort any command currently running in DraftSight
'to avoid nested commands
dsApp.<b>AbortRunningCommand</b> </pre>
<pre> 'Get active document
Dim dsDoc As DraftSight.Document
Set dsDoc = dsApp.<b>GetActiveDocument</b>()
If dsDoc Is Nothing Then
MsgBox ("There are no open documents in DraftSight.")
Return
End If </pre>
<pre> 'Get how transparency is defined
Dim dsLayerManager As DraftSight.LayerManager
Dim dsLayer As DraftSight.Layer
Set dsLayerManager = dsDoc.<b>GetLayerManager</b>
Set dsLayer = dsLayerManager.<b>GetActiveLayer</b>
Debug.Print "Transparency percentage for active Layer: " & dsLayer.<b>Transparency</b>
'Set new transparency percentage for the active Layer
dsLayer.<b>Transparency</b> = 50
Debug.Print "New transparency percentage for active Layer: " & dsLayer.<b>Transparency</b></pre>
<pre> 'Get the TableStyle Manager
Dim dsTableStyleManager As DraftSight.TableStyleManager
Set dsTableStyleManager = dsDoc.<b>GetTableStyleManager</b> </pre>
<pre> 'Create the TableStyle
Dim dsTableStyle As DraftSight.TableStyle
Dim sampleTableStyle As String
Dim status As Long
sampleTableStyle = "Sample Table Style"
dsTableStyleManager.<b>CreateTableStyle</b> sampleTableStyle, dsTableStyle, status </pre>
<pre> 'Set the TableStyle styles
'Create the Table in a downward direction
dsTableStyle.<b>HeaderOrientation</b> = dsTableHeaderOrientation_Down</pre>
<pre> 'Set the Colors for the Table backgrounds
Dim dsColorHeader As DraftSight.Color
Dim dsColorData As DraftSight.Color
Dim dsColorTitle As DraftSight.Color </pre>
<pre> Set dsColorHeader = dsTableStyle.<b>GetBackgroundColor</b>(dsTableCellType_Header)
dsColorHeader.<b>SetNamedColor</b> dsNamedColor_e.dsNamedColor_Cyan
dsTableStyle.<b>SetBackgroundColor</b> dsTableCellType_Header, dsColorHeader
</pre>
<pre> Set dsColorData = dsTableStyle.<b>GetBackgroundColor</b>(dsTableCellType_Data)
dsColorData.<b>SetNamedColor</b> dsNamedColor_e.dsNamedColor_Green
dsTableStyle.<b>SetBackgroundColor</b> dsTableCellType_Data, dsColorData
</pre>
<pre> Set dsColorTitle = dsTableStyle.<b>GetBackgroundColor</b>(dsTableCellType_Title)
dsColorTitle.<b>SetNamedColor</b> dsNamedColor_e.dsNamedColor_White
dsTableStyle.<b>SetBackgroundColor</b> dsTableCellType_Title, dsColorTitle</pre>
<pre> 'Activate the TableStyle
dsTableStyle.<b>Activate</b> </pre>
<pre> 'Get the model document
Dim dsModel As DraftSight.Model
Set dsModel = dsDoc.<b>GetModel</b> </pre>
<pre> 'Get the Sketch Manager
Dim dsSketchManager As DraftSight.SketchManager
Set dsSketchManager = dsModel.<b>GetSketchManager</b> </pre>
<pre> 'Insert the Table
Dim dsTable As DraftSight.Table
Dim x As Double
x = 6
Dim y As Double
y = 6
Dim row As Long
row = 4
Dim col As Long
col = 5
Dim rowHeight As Double
rowHeight = 2
Dim colWidth As Double
colWidth = 4
Set dsTable = dsSketchManager.<b>InsertTable</b>(x, y, row, col, rowHeight, colWidth, dsTableCellType_Title, dsTableCellType_Header, dsTableCellType_Data)</pre>
<pre> 'Get transparency object for Table
Dim dsTransparency As DraftSight.Transparency
Set dsTransparency = dsTable.<b>Transparency</b>
'Find out if Table's transparency is ByLayer
Debug.Print "Transparency defined ByLayer: " & dsTransparency.<b>IsByLayer</b>
'If not, then change Table's transparency to ByLayer and
'update Table's transparency object
If Not dsTransparency.<b>IsByLayer</b> Then
dsTransparency.<b>SetByLayer</b>
Debug.Print "Transparency now ByLayer: " & dsTransparency.<b>IsByLayer</b>
'Update Table's transparency object
dsTable.<b>Transparency</b> = dsTransparency
End If </pre>
<pre> 'Change Table's top row's background color
dsColorTitle.<b>SetNamedColor</b> (dsNamedColor_Yellow)
dsTable.<b>SetBackgroundColor</b> dsTableCellType_Title, dsColorTitle
'Change background color of cell in second row and
'second column to match the top row's background color
dsTable.<b>SetCellBackgroundColor</b> 1, 1, dsColorTitle
'Get the name of the TableStyle for the just-inserted Table
Debug.Print ("TableStyle applied to this table: " & dsTableStyle.<b>Name</b>)
dsApp.<b>Zoom</b> dsZoomRange_Fit, Nothing, Nothing
End Sub</pre>
</body>
</html>