naming newly created features

Programming and macros
denglo
Posts: 2
Joined: Tue May 24, 2022 9:07 pm
Answers: 0

naming newly created features

Unread post by denglo »

Hey there,

As i'll have to do several weeks of weldments I've decided to write a few macros to make my live easier. At the moment my test macro creates flange as an offset feature onto the selected surface.
Now when I want the Macro to assign a material to that newly created body I'll have to know the name of the new feature, right?

so my idea atm is:
step 1: browse feature tree for features named flange-n find first available number
create new flange (sheet metal without merging)
assign material to new flange.

the interesting bits of code:

Code: Select all

Dim myFeature As Object
Set myFeature = Part.FeatureManager.InsertSheetMetalBaseFlange2(dblthickness, False, 0.003, 0.02, 0.01, False, 0, 0, 1, customBendAllowanceData, False, 0, 0.0001, 0.0001, 0.5, True, False, True, True)


boolstatus = Part.Extension.SelectByID2("Sheet<1>", "SUBWELDFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Base-Flange1", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)

boolstatus = Part.Extension.SelectByID2("Base-Flange1", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
Part.SetMaterialPropertyName2 "Default<As Machined>", "C:/Program Files/SOLIDWORKS Corp/SOLIDWORKS/lang/english/sldmaterials/SOLIDWORKS DIN Materials.sldmat", "1.0045 (S355JR)"
How do I give the new feature a name?

Also that feature overwrites the standard thickness for sheet metal parts, so lets say i create one flange with 5 and one with 10. and ill have two with 10mm. any ideas how to solve that?
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 278
x 463

Re: naming newly created features

Unread post by AlexB »

There is a lot you can do to get what you want. Typically, when you create a feature, the feature object is given as the return value of the function. Using that, you can use
image.png
to get the body(bodies) that belongs to that feature. There are other ways, but that's the most straight forward.

To name a feature:
https://help.solidworks.com/2012/englis ... ~Name.html
denglo
Posts: 2
Joined: Tue May 24, 2022 9:07 pm
Answers: 0

Re: naming newly created features

Unread post by denglo »

hey, thanks for that. still getting used to finding the right API calls and then working with them correctly. (haven't programmed in about 10 years)

So here is how I've changed, and it works fine, as long as i dont change the order of the new features (obviously):

Code: Select all

' check for available n
n = 1
Set swfeature = swpart.FirstFeature
Do While Not swfeature Is Nothing
    If swfeature.Name = "flange" & n Then
        n = n + 1
    End If
    Set swfeature = swfeature.GetNextFeature
Loop

Dim customBendAllowanceData As Object
Set customBendAllowanceData = swpart.FeatureManager.CreateCustomBendAllowance()
customBendAllowanceData.KFactor = 0.5

Dim myFeature As SldWorks.Feature
Set myFeature = swpart.FeatureManager.InsertSheetMetalBaseFlange2(dblthickness, False, 0.003, 0.02, 0.01, False, 0, 0, 1, customBendAllowanceData, False, 0, 0.0001, 0.0001, 0.5, True, False, True, True)
myFeature.Name = "flange" & n

'boolstatus = swpart.Extension.SelectByID2("Sheet<1>", "SUBWELDFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swpart.Extension.SelectByID2("flange" & n, "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
swpart.SetMaterialPropertyName2 "Default<As Machined>", "C:/Program Files/SOLIDWORKS Corp/SOLIDWORKS/lang/english/sldmaterials/SOLIDWORKS DIN Materials.sldmat", "1.0045 (S355JR)"
is there an easier way for a sub procedure that checks all features for the next free name without them being in order? I mean, i have a concept in my head, but its definitely not elegant
Post Reply