Get Annotations Arrays Example (VB.NET)
Before SolidWorks 2009 SP1, API programmers used IView::GetFirst<Annotation> and I<Annotation>::GetNext
methods to traverse the annotations in a document view. New
methods on IView now return entire annotation arrays that programmers
can iterate through to obtain all of the annotations in a document view.
This code example does the following:
builds a simple part with the following annotations:
display
dimensions, geometric tolerance symbol, surface finish symbol, datum tag
symbol, datum target symbol
creates a drawing sheet with 3rd angle views of
the part and its annotations
calls the new methods on IView to get arrays of
each annotation type in the views (bold text)
iterates through each annotation array and pops
up a message box with information about each annotation in the drawing
views
'---------------------------------------
' Preconditions:
' Document templates are installed at this location:
' C:\Documents
and Settings\All Users\Application Data\SolidWorks\SolidWorks 2009\templates\
'
' Postconditions:
' Several message boxes pop up containing information
about each annotation
' in the drawing views
'----------------------------------------
Option Explicit On
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Partial Class SolidWorksMacro
Const
filedir As String = "C:\Documents and Settings\All Users\Application
Data\SolidWorks\SolidWorks 2009\templates\"
Const
TemplateName As String = "C:\Documents and Settings\All Users\Application
Data\SolidWorks\SolidWorks 2009\templates\drawing.drwdot"
Const
TemplateSize As Long = swDwgTemplates_e.swDwgTemplateBsize
Const
PaperSize As Long = swDwgPaperSizes_e.swDwgPaperBsize
Const
ScaleNum As Double = 1.0#
Const
ScaleDenom As Double = 2.0#
Dim
swModel As ModelDoc2
Dim
swFeatMgr As FeatureManager
Dim
Response As Integer
Dim
ThirdAngle As Boolean
Dim
swDraw As DrawingDoc
Dim
swSheet As Sheet
Dim
i As Long
Dim
j As Long
Dim
retval As Boolean
Dim
swView As View
Dim
count As Long
Dim
Annotations As Object
Dim
msg As String
Public
Sub main()
'Create
a part
Build_Part()
'Create
a drawing view of the part
swModel
= swApp.ActiveDoc
swDraw
= swApp.NewDocument(TemplateName, PaperSize, _
0.0#,
0.0#)
ThirdAngle
= False
swDraw.ActivateSheet("Sheet1")
swSheet
= swDraw.GetCurrentSheet
swSheet.SetSize(PaperSize,
0.0#, 0.0#)
swSheet.SetScale(ScaleNum,
ScaleDenom, True, True)
'
Create 3rd angle drawing views.
'
The following method pops up a save part dialog.
'
This macro continues only after you click Save in the dialog.
retval
= swDraw.Create3rdAngleViews2(swModel.GetPathName)
Dim
LocX As Double
Dim
LocY As Double
LocX
= 0.2635088599471
LocY
= 0.1934578136726
'
Create isometric drawing view
swDraw.CreateDrawViewFromModelView3(swModel.GetPathName,
"*Isometric", LocX, LocY, 0)
swDraw.ViewDisplayShaded()
'
Insert display dimension annotations from the current model
swDraw.InsertModelAnnotations3(0,
swInsertAnnotation_e.swInsertDimensionsMarkedForDrawing, True, True, False,
True)
'
Insert datum target symbol annotations from the current model
swDraw.InsertModelAnnotations3(0,
swInsertAnnotation_e.swInsertDatumTargets, True, True, False, True)
'
Insert geometric tolerance annotations from the current model
swDraw.InsertModelAnnotations3(0,
swInsertAnnotation_e.swInsertGTols, True, True, False, True)
'
Insert surface finish symbol annotations from the current model
swDraw.InsertModelAnnotations3(0,
swInsertAnnotation_e.swInsertSFSymbols, True, True, False, True)
'
Insert datum tag annotations from the current model
swDraw.InsertModelAnnotations3(0,
swInsertAnnotation_e.swInsertDatums, True, True, False, True)
'
Insert dowel symbol on a selected arc or circle - not applicable to this
model
'swDraw.InsertDowelSymbol
'
Insert multi-jog leader
'swDraw.InsertMultiJogLeader3
'
Insert datum origin
'
Insert center line on a selected entity
'swDraw.InsertCenterLine2
'
Insert cosmetic thread
'swDraw.InsertThreadCallout
'
Insert weld symbol on the last edge selection
'swDraw.InsertWeldSymbol
'
Insert weld bead
'
Insert table
'swDraw.InsertTableAnnotation2
swDraw.ForceRebuild3(True)
'
Iterate through all the views on the drawing to find display dimensions
swView
= swDraw.GetFirstView
Do
While Not swView Is Nothing
count
= swView.GetDisplayDimensionCount
Dim
DisplayDimension As DisplayDimension
Dim
swDim As Dimension
'
Iterate through all the display dimension annotations in each drawing
view that has them
If
count > 0 Then
Annotations
= swView.GetDisplayDimensions
For
j = 0 To UBound(Annotations)
DisplayDimension
= Annotations(j)
swDim
= DisplayDimension.GetDimension
'
For each annotation in each drawing view, pop up a message box with the
'
view:annotation name and corresponding dimension
msg
= "Display dimension found: " & swView.GetName2 & ":"
& DisplayDimension.GetAnnotation.GetName & " = " _
&
swDim.GetSystemValue2("") & " meters"
swApp.SendMsgToUser2(msg,
swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
Next
j
End
If
swView
= swView.GetNextView
Loop
'
Iterate through all the views on the drawing to find datum target symbols
swView
= swDraw.GetFirstView
Do
While Not swView Is Nothing
count
= swView.GetDatumTargetSymCount
Dim
dtsymbol As DatumTargetSym
'
Iterate through all the datum target symbol annotations in each drawing
view that has them
If
count > 0 Then
Annotations
= swView.GetDatumTargetSyms
For
j = 0 To UBound(Annotations)
dtsymbol
= Annotations(j)
'
For each annotation in each drawing view, pop up a message box with the
'
view:annotation name and name of each datum target symbol found
msg
= "Datum target symbol found: " & swView.GetName2 &
":" & dtsymbol.GetAnnotation.GetName
swApp.SendMsgToUser2(msg,
swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
Next
j
End
If
swView
= swView.GetNextView
Loop
'
Iterate through all the views on the drawing to find surface finish symbols
swView
= swDraw.GetFirstView
Do
While Not swView Is Nothing
count
= swView.GetSFSymbolCount
Dim
sfsymbol As SFSymbol
'
Iterate through all the surface finish symbol annotations in each drawing
view that has them
If
count > 0 Then
Annotations
= swView.GetSFSymbols
For
j = 0 To UBound(Annotations)
SFSymbol
= Annotations(j)
'
For each annotation in each drawing view, pop up a message box with the
'
view:annotation name and name of each surface finish symbol found
msg
= "Surface finish symbol found: " & swView.GetName2 &
":" & SFSymbol.GetAnnotation.GetName
swApp.SendMsgToUser2(msg,
swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
Next
j
End
If
swView
= swView.GetNextView
Loop
'
Iterate through all the views on the drawing to find datum tags
swView
= swDraw.GetFirstView
Do
While Not swView Is Nothing
count
= swView.GetDatumTagCount
Dim
dTag As DatumTag
'
Iterate through all the datum tags in each drawing view that has them
If
count > 0 Then
Annotations
= swView.GetDatumTags
For
j = 0 To UBound(Annotations)
dTag
= Annotations(j)
'
For each annotation in each drawing view, pop up a message box with the
'
view:annotation name and name of each datum tag found
msg
= "Datum tag found: " & swView.GetName2 & ":"
& dTag.GetAnnotation.GetName
swApp.SendMsgToUser2(msg,
swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
Next
j
End
If
swView
= swView.GetNextView
Loop
'
Iterate through all the views on the drawing to find geometric tolerances
swView
= swDraw.GetFirstView
Do
While Not swView Is Nothing
count
= swView.GetGTolCount
Dim
gtol As Gtol
'
Iterate through all the geometric tolerance symbols in each drawing view
that has them
If
count > 0 Then
Annotations
= swView.GetGTols
For
j = 0 To UBound(Annotations)
Gtol
= Annotations(j)
'
For each annotation in each drawing view, pop up a message box with the
'
view:annotation name and name of each geometric tolerance found
msg
= "Geometric tolerance symbol found: " & swView.GetName2
& ":" & Gtol.GetAnnotation.GetName
swApp.SendMsgToUser2(msg,
swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
Next
j
End
If
swView
= swView.GetNextView
Loop
'
In a similar fashion:
'
Get other annotations on the drawing, if they exist.
'
Iterate through all the views on the drawing.
'
Get the annotation count, and if greater than zero, get the annotation
array.
'
Iterate on each array, and Set an annotation object to each array member:
'
Annotations = swView.GetDowelSymbols
'
Annotations = swView.GetMultiJogLeaders
'
Annotations = swView.GetDatumOrigins
'
Annotations = swView.GetCenterLines
'
Annotations = swView.GetCThreads
'
Annotations = swView.GetWeldSymbols
'
Annotations = swView.GetWeldBeads
'
Annotations = swView.GetTableAnnotations
swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate,
True)
swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayAnnotations,
False)
End
Sub
Private
Sub Build_Part()
'Builds
a simple part with the following annotations:
'display
dimensions, geometric tolerance symbol, surface finish symbol, datum tag
symbol, datum target symbol
swModel
= swApp.NewDocument(filedir + _
"part.prtdot",
0, 0.0#, 0.0#)
swModel.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swUnitsLinear,
swLengthUnit_e.swMETER)
swModel.SetUserPreferenceDoubleValue(
_
swUserPreferenceDoubleValue_e.swMaterialPropertyDensity,
7800)
swModel.SetUserPreferenceStringValue(
_
swUserPreferenceStringValue_e.swMaterialPropertyCrosshatchPattern,
"ISO (Steel)")
swModel.SketchManager.InsertSketch(False)
Dim
Height As Double
Dim
Width As Double
Height
= 0.05
Width
= 0.05
swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate,
False)
swModel.SketchManager.CreateLine(0.01,
0.01, 0, 0.01, _
0.01
+ Height, 0)
swModel.ViewZoomtofit2()
'
Add display dimension to line
swModel.AddDimension2(0,
0.01 + Height / 2, 0)
'
Add geometric tolerance to line
swModel.InsertGtol()
swModel.SketchManager.CreateLine(0.01,
0.01, 0, 0.01 + Width, _
0.01,
0)
swModel.ViewZoomtofit2()
'
Add display dimension
swModel.AddDimension2(0.01
+ Width / 2, 0, 0)
'
Add surface finish symbol to line
swModel.Extension.InsertSurfaceFinishSymbol3(swSFSymType_e.swSFBasic,
swLeaderStyle_e.swSTRAIGHT, 0.01, 0.01, 0.01, swSFLaySym_e.swSFCircular,
swArrowStyle_e.swDOT_ARROWHEAD, "", "", "",
"", "", "", "")
swModel.SketchManager.CreateLine(0,
0, 0, 0, 0.01, 0). _
ConstructionGeometry
= True
swModel.ClearSelection2(True)
swModel.ViewZoomtofit2()
Dim
Thick As Double
Thick
= 0.05
Dim
Depth As Double
Depth
= 0.05
swFeatMgr
= swModel.FeatureManager
swFeatMgr.FeatureExtrusionThin2(True,
False, True, 0, 0, _
Depth,
0, False, False, False, False, 0, 0, False, _
False,
False, False, False, Thick, 0, 0, 0, 0, False, _
False,
False, False, swStartConditions_e.swStartSketchPlane, 0.0#, False)
swModel.ViewZoomtofit2()
swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayAnnotations,
True)
swModel.ShowNamedView2("Isometric",
7)
swModel.ViewZoomtofit2()
'
Add datum tag to line
retval
= swModel.Extension.SelectByID2("", "EDGE", 0.06001738353251,
-0.01284975502705, -0.05001738353241, False, 0, Nothing, 0)
Dim
dTag As DatumTag
dTag
= swModel.InsertDatumTag2
'
Add datum target symbol to line
retval
= swModel.Extension.SelectByID2("", "EDGE", 0.06001738353251,
-0.01284975502705, -0.05001738353241, False, 0, Nothing, 0)
Dim
myDatumTarget As Object
myDatumTarget
= swModel.Extension.InsertDatumTargetSymbol2("", "",
"", 0, False, 0.03, 0.03, "", "", True,
12, 0, False, True, True)
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class