Fix errors when creating sketch segments using SOLIDWORKS API
Symptoms
SOLIDWORKS macro creates sketch segments (line, arcs, etc) or sketch points using SOLIDWORKS API. And in some cases the elements are not created while it works correct in other cases.
Cause
By default all entities inserted using the ISketchManager interface are created via user interface. That means that the entity cannot be created if the target area (i.e. boundaries of the segments) is not visible in the user interface (e.g. the view is moved or scaled).
Resolution
Set ISketchManager::AddToDB property to True before the entities creation and restore the original value once the job is finished. Setting this option to true will bypass the creation of entities via User Interface rather add the data directly to the model storage. This may also improve performance while creating the entities.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim addToDbOrig As Boolean
addToDbOrig = swModel.SketchManager.AddToDB 'get the original value
swModel.SketchManager.AddToDB = True
swModel.SketchManager.CreateLine 0, 0, 0, 0.01, 0.02, 0
swModel.SketchManager.AddToDB = addToDbOrig 'restore the original value
End Sub