Files
Demonstrate of safe entities
Code
'This macro examines whether storing a face as an entity and then inserting a component _
causes the entity pointer to be lost, as discussed in the API Fundamentals _
manual: "If you try to store a pointer to a SldWorks::Entity object from an assembly _
and try to use it for selection after a component has been added to an assembly, _
the entity pointer becomes invalid."
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Preconditions: Assembly is open with a component, and a face is selected
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swFace As SldWorks.Face2
Dim swFaceEnt As SldWorks.Entity
Dim swFaceSafeEnt As SldWorks.Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim swComp As SldWorks.Component2
Dim errors As Long
Dim warnings As Long
Public Const strCompPath As String = "C:SolidWorks Training FilesEssentialsLesson04ExercisesBuilt PartsTool Holder_&.SLDPRT"
Sub OpenCompModel()
swApp.DocumentVisible False, swDocPART
swApp.OpenDoc6 strCompPath, 1, 0, "", errors, warnings
swApp.DocumentVisible True, swDocPART
End Sub
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swAssy = swModel
Set swSelData = swSelMgr.CreateSelectData
Set swFace = swSelMgr.GetSelectedObject6(1, -1)
Set swFaceEnt = swFace
Set swFaceSafeEnt = swFaceEnt.GetSafeEntity
Debug.Print "Is the entity safe? " & swFaceEnt.IsSafe
swModel.ClearSelection2 True 'clear the selection
'====Put code here that would cause pointer to be lost====
'1. If the face was selected while you were in edit part and _
you then go back to the assembly, is the pointer kept?
'swAssy.EditAssembly 'answer: yes
'2. If the face was selected and you add another component, _
is the pointer kept?
'Call OpenCompModel
'Set swComp = swAssy.AddComponent4(strCompPath, "", 0.01, 0.01, 0.01)
'3. EditRebuild3
'swModel.EditRebuild3
'4. ForceRebuild3
swModel.ForceRebuild3 False
'5. EditDelete (will obviously cause error)
'swModel.EditDelete
'=========================================================
'try to select
On Error GoTo ErrHandling:
swFaceEnt.Select4 False, swSelData
Exit Sub
ErrHandling:
Debug.Print "Entity object pointer: " & Hex(ObjPtr(swFaceEnt))
Debug.Print Err.Number
Debug.Print Err.Description
End Sub