Insert and Position DXF File in Drawing Example (VBA)
This examples shows how to insert and position a DXF file in a drawing.
'----------------------------------------------------
'
' Preconditions:
' (1)
Drawing is open.
' (2)
Specified DXF file exists.
' (3)
Sheet where you want to insert the DXF file is selected.
'
' Postconditions: Specified DXF file inserted and positioned
as a drawing view.
'
'-----------------------------------------------
Option Explicit
Sub main()
Const
sDwgFileName As
String = "D:\samples\rainbow.DXF"
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swDraw As
SldWorks.DrawingDoc
Dim
swFeatMgr As
SldWorks.FeatureManager
Dim
swFeat As
SldWorks.feature
Dim
swSketch As
SldWorks.sketch
Dim
swView As
SldWorks.View
Dim
vPos As
Variant
Dim
bRet As
Boolean
Dim
nRetVal As
Long
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swDraw = swModel
Set
swFeatMgr = swModel.FeatureManager
Set
swFeat = swFeatMgr.InsertDwgOrDxfFile(sDwgFileName)
Set
swSketch = swFeat.GetSpecificFeature2
Set
swView = swDraw.GetFirstView
Do
While Not swView Is Nothing
If
swSketch Is swView.GetSketch Then
Exit
Do
End
If
Set
swView = swView.GetNextView
Loop
vPos
= swView.position
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Sketch
=
" & swFeat.Name
Debug.Print
" View
=
" & swView.Name
Debug.Print
" Old
Pos =
(" & vPos(0) * 1000# & ", " & vPos(1) * 1000#
& ") mm"
'
Move to right
vPos(0)
= vPos(0) + 0.01
swView.position = vPos
vPos
= swView.position
Debug.Print
" New
Pos =
(" & vPos(0) * 1000# & ", " & vPos(1) * 1000#
& ") mm"
'
Redraw
swModel.GraphicsRedraw2
End Sub
'----------------------------------------------------