Create Plane thru 3 Points In-context Example (VBA)
This example shows how to create a plane through 3 points in-context.
'----------------------------------------
'
' Preconditions:
' (1)
Assembly is open.
' (2)
Assembly is fully resolved.
' (3)
Component is selected.
'
' Postconditions: Plane, passing through three points,
' is
created in the selected component.
'
'----------------------------------------
Option Explicit
' Possible
status values for AssemblyDoc::EditPart2
Public Enum swEditPartCommandStatus_e
swEditPartFailure
= -1
swEditPartAsmMustBeSaved
= -2
swEditPartCompMustBeSelected
= -3
swEditPartCompMustBeResolved
= -4
swEditPartCompMustHaveWriteAccess
= -5
swEditPartSuccessful
= 0
swEditPartCompNotPositioned
= &H1
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swEditModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swSelData As
SldWorks.SelectData
Dim
swPart As
SldWorks.PartDoc
Dim
swSketchPt1 As
SldWorks.SketchPoint
Dim
swSketchPt2 As
SldWorks.SketchPoint
Dim
swSketchPt3 As
SldWorks.SketchPoint
Dim
swPlane As
SldWorks.refPlane
Dim
nRetVal As
Long
Dim
nInfo As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swSelData = swSelMgr.CreateSelectData
Set
swAssy = swModel
'
start in-context edit
nRetVal
= swAssy.EditPart2(True, False,
nInfo)
Debug.Assert
swEditPartSuccessful = nRetVal
Set
swEditModel = swModel
'
Turn off snapping
swEditModel.SetAddToDB True
'
Insert part/component 3D sketch in-context
swEditModel.Insert3DSketch2 True
'
Create points in part
Set
swSketchPt1 = swEditModel.CreatePoint2(0#,
0.02123307340457, 0.005485856156458)
Set
swSketchPt2 = swEditModel.CreatePoint2(0.04415646169588,
0.01166034702997, -0.00770979679615)
Set
swSketchPt3 = swEditModel.CreatePoint2(0#,
-0.006247647329005, 0.007641244473859)
'
Exit sketch but in assembly
'
This gets you to editing part/component in-context
swModel.Insert3DSketch2 True
'
Restore snapping
swEditModel.SetAddToDB False
swModel.ClearSelection2 True
bRet
= swSketchPt1.Select4(True, swSelData):
Debug.Assert bRet
bRet
= swSketchPt2.Select4(True, swSelData):
Debug.Assert bRet
bRet
= swSketchPt3.Select4(True, swSelData):
Debug.Assert bRet
'
Create plane in part/component
Set
swPlane = swModel.CreatePlaneThru3Points3(True)
Debug.Assert
Not swPlane Is Nothing
'
Go back to assembly
'
End in-context edit
swAssy.EditAssembly
End Sub
'----------------------------------------