Hello,
I would like to write a macro that creates a new plane with a given distance dynamically, and then starts a 2D sketch on it. Therefore I need to select the newly created plane before I start the sketch. I couldn't understand how to choose a newly created plane.
My current code looks like this:
Dim myRefPlane As Object
Distance = Distance / 1000
Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, Distance, 0, 0, 0, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
The code above just assumes that the new plane is called Plane1, however, this might not be the case. Any ideas?
Thank you
Get the last added feature from the ModelDocExtension object.
Andreas.
Option Explicit
Sub Main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExt As SldWorks.ModelDocExtension
Dim Distance
'Refer to the current model
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swExt = swModel.Extension
'Be sure a plane is selected
Dim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swModel.SelectionManager
With swSelMgr
If .GetSelectedObjectCount2(-1) = 0 Then
MsgBox "Select a plane and try again"
End If
If .GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelDATUMPLANES Then
MsgBox "Select a plane and try again"
End If
End With
'Ask for the distance
Distance = InputBox("Distance in mm:")
'Abort?
If Not IsNumeric(Distance) Then Exit Sub
'Change to meters
Distance = Distance / 1000
'Insert the new plane
Dim myRefPlane As SldWorks.RefPlane
Set myRefPlane = swModel.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, Distance, 0, 0, 0, 0)
'Get the last feature
Dim swFeature As SldWorks.Feature
Set swFeature = swExt.GetLastFeatureAdded
'Select it
swFeature.Select2 False, -1
'Insert a sketch
swModel.SketchManager.InsertSketch True
End Sub