SelectbyID - is there an easier way to select the last item
SelectbyID - is there an easier way to select the last item
(OP)
I am trying to create VBA code to automate building a model.
I am having problems with the code for SelectByID2.
The problem is that I do not always know the exact ID name of the last item in the feature tree. Is there a simpler way to select the last item in the tree regardless of what it is?
I am having problems with the code for SelectByID2.
The problem is that I do not always know the exact ID name of the last item in the feature tree. Is there a simpler way to select the last item in the tree regardless of what it is?






RE: SelectbyID - is there an easier way to select the last item
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
Does it return a string, the api help isnt ver helpful
RE: SelectbyID - is there an easier way to select the last item
GetLastFeatureAdded will return a feature object. And it's part of the ModelDocExtension. So to use it you would do something like
dim swext as sldworks.modeldocextension
dim myfeat as sldworks.feature
set swext = application.sldworks.activedoc.extension
set myfeat = swext.getlastfeatureadded
Here are the remarks from the 2007 help file:
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
Ok I am missing something
I have set up the following new macro
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
Dim swext As SldWorks.ModelDocExtension
Dim myfeat As SldWorks.Feature
Set swext = Application.SldWorks.ActiveDoc.Extension
Set myfeat = swext.GetLastFeatureAdded
End Sub
How do i get the value returned to me? Dont i need to set up some sort of string?
RE: SelectbyID - is there an easier way to select the last item
myfeat.select2 true, 1
"true" will add myfeat to the current selection set, false will un-select everything that is selected and select only myfeat
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
RE: SelectbyID - is there an easier way to select the last item
It does select the item, but I guess it still doesnt solve my problem.
I was trying to use this method to select the item instead of a statement like
boolstatus = Part.Extension.SelectByID2("Extrude1", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0)
because i dont always know if the feature is called "Extrude1" or "Cavity1" or something else
In the next line of code, after I select the feature I am trying to rotate teh body about the x axis using the code
Part.FeatureManager.InsertMoveCopyBody2 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.22, False, 1 , Nothing,0)
the boolstatus line must select it differently than the myfeat.select2 line because although I see the feature selected, the next line does not execute properly using the myfeat line.
Is there a diffent approach that I should use?
RE: SelectbyID - is there an easier way to select the last item
If you want to rotate all bodies affected by the last feature in the tree, I would suggest:
Feature::GetAffectedFaces to get all faces affected by the feature
Face2::GetBody to get the body of each face
Body2::Select to select each body. If you use True for the append argument, all bodies affected by the last feature in the feature tree will be selected. You can then do your InsertMoveCopyBody function.
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
I have only one body to rotate. I am not sure of the syntax needed - can you please clarify again forme?
RE: SelectbyID - is there an easier way to select the last item
dim myFaces as variant
dim myFace as Face2
dim myBody as body2
myfaces = myfeat.getaffectedfaces 'Returns array of faces
set myface = myfaces(0) 'get first face in array. We assume only one body.
set mybody = myface.getbody
mybody.select false, 1
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
I still cant get it to run
any suggetions?
Sub main()
Dim swApp As Object
Dim myFaces As Variant
Dim myFace As Face2
Dim myBody As Body2
Dim myfeat As SldWorks.Feature
Set swApp = Application.SldWorks
myFaces = myfeat.GetAffectedFaces 'Returns array of faces
Set myFace = myFaces(0) 'get first face in array. We assume only one body.
Set myBody = myFace.GetBody
myBody.Select False, 1
Part.FeatureManager.InsertMoveCopyBody2 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.22, False, 1
End Sub
RE: SelectbyID - is there an easier way to select the last item
-handleman, CSWP (The new, easy test)
RE: SelectbyID - is there an easier way to select the last item
thank you so much for your time