Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IRstuff on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectbyID - is there an easier way to select the last item

Status
Not open for further replies.

Spurs

Mechanical
Nov 7, 2002
297
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?



 
Replies continue below

Recommended for you

Yes. I have never found SelectByID very useful for anything. There are examples in the API help for traversing the feature tree. And I think there's even a function (method of the FeatureManager object?) called GetLastFeatureAdded.

-handleman, CSWP (The new, easy test)
 
What syntax do you use for this method?

Does it return a string, the api help isnt ver helpful

 
Are you using 2009? If so, they redid the help file and it absolutely sucks. If at all possible, I would recommend getting hold of the 2008 API help and using 2009's only in case of emergency. That'll also make sure your code is backward-compatible. ;-)

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:

API Help said:
This method can access subfeature types such as mates, drawing views, and so on. For example, if you just added the first coincident mate to MateGroup, then ModelDocExtension::GetLastFeatureAdded will return Coincident1. ModelDoc2::FeatureByPositionReverse will return MateGroup.

-handleman, CSWP (The new, easy test)
 
Handleman
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?
 
What exactly do you want to do with the feature? Is the end requirement to select it, or to do something else? You can do almost anything to a feature from its handle without selecting it. However, if you really just want to select it, after your "Set myfeat..." line, put

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)
 
Yes I was trying to select - this works great - thanks for your assistance
 
I was too hasty to say it works.

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?
 
That's why I asked what you were trying to do. This methodology won't work from the user interface either. If you select the last feature in the feature tree and then start the move/copy body command, you will see that nothing is selected to be moved/copied. In order to move/copy bodies, you have to select bodies. I'm guessing you got that SelectByID2 line by recording a macro? And you selected a face of the body you were trying to rotate in the graphics area? The reason that "Extrude1" is in that SelectByID2 line is that is the name of the body owning the face you selected. It's the name of that body because a body takes on the name of whatever feature last acted upon it.

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)
 
Handleman - you are correct that I recored the macro - which of course dosnt help alot sometims.

I have only one body to rotate. I am not sure of the syntax needed - can you please clarify again forme?

 
If you are certain you will only have one body, I believe you could use (in addition to above)

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)
 
Below is my simplied program

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
 
You need to keep all that previous code in addition to the new. Once you put that in, change GetAffectedFaces to GetFaces (sorry about that) and change part.featuremanager.... to swapp.activedoc.featuremanager....

-handleman, CSWP (The new, easy test)
 
It works great

thank you so much for your time

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor