What's new

I have in mind to now look at creating a bolt/washer/washer/nut sub assembly

albie0803

Alibre Super User
So, coding is not my strong point and I find that the reference doesn't give examples of workflow.

Assy = CurrentAssembly()
Assy.AddNewSubAssembly("BoltSet",0,0,0)

so now how do I make Boltset the new current assembly? Is it automatically now current?

BoltSet.AddNewPart("Bolt",0,0,0)

Once I have finished building my subassembly, how do I then step back to the main assembly?

More questions will come :rolleyes:
 

NateLiquidGravity

Alibre Super User
Look at what is returned by functions/methods. Assign the return to a variable to use it later.
I'm not at my pc right now but I think this is correct:
Code:
Assy = CurrentAssembly()
BoltSet = Assy.AddNewSubAssembly("BoltSet",0,0,0) # Assign the new subassembly to the variable BoltSet
Bolt = BoltSet.AddNewPart("Bolt",0,0,0) # Assign the new part to the variable Bolt
It's all "current" just use the right variable.
 

albie0803

Alibre Super User
Not quite, the above code gives the following error:

File "<string>", line 3, in <module>
Exception: Unable to add new part to assembly. Make sure the assembly has been opened for editing. Operation is invalid as the parent Occurrence is not active
 

idslk

Alibre Super User
my first thought is create a new assembly with the script, save it temporarily and load it as a sub assembly instead of creating it inside the main assembly. Don't know if it works...
 

ajayre

Alibre Super User
Create a new assembly - this will open a new assembly window.
In there put together your subassembly.
Then save and close the subassembly.
In CurrentAssembly() add the newly created subassembly.

I.e. "bottom-up" approach.

Andy
 

idslk

Alibre Super User
Code:
ca = CurrentAssembly()
print 'CurrentAssembly',ca
subassy = Assembly('BoltSet')
print 'NewSubAssembly',subassy
bolt = subassy.AddNewPart("Bolt",0,0,0) # Assign the new part to the variable Bolt
print 'NewPart in Subassembly',bolt
boltsketch = bolt.AddSketch('boltsketch',subassy.XYPlane)
print 'Sketchname',boltsketch.Name
circle = boltsketch.AddCircle(0,0,10,False)
print 'added Circle',circle
circlebody = bolt.AddExtrudeBoss('CircleBody',bolt.GetSketch('boltsketch'),20,False)
print 'created Body',circlebody

# You have to insert your save location instead of *******
subassy.Save(r"C:/Users/*******")
loadedassy = Assembly(r"C:/Users/*******/","BoltSet.ad_asm")


print loadedassy
sub_assy = ca.AddSubAssembly(loadedassy,0,0,0)
print sub_assy
win = Windows()
win.InfoDialog('Wait for save operation to be fullfilled...','Wait')
win.InfoDialog('...only to be sure before trying to close subassy and part window','Wait')
print 'close loaded',loadedassy
loadedassy.Close()
print 'close sub',subassy
subassy.Close()
 

albie0803

Alibre Super User
@ajayre I use M-Files. Is there code to switch on/off "Use Vault" So I can save the subassembly to a set location before importing the completed assembly into the main assembly, where it will get saved to M-Files when I save the whole model or does the save function just used the coded location and ignore M-Files?

@idslk Thankyou so much for the code to get me started. Here is my WIP

Code:
# ca = existing Assembly
ca = CurrentAssembly()
print 'CurrentAssembly:',ca

# New SubAssembly (suba)
subassy = Assembly('BoltSet')
print 'NewSubAssembly:',subassy

# Add bolt to subassambly
bolt = subassy.AddNewPart("Bolt",0,0,0) # Assign the new part to the variable Bolt
print 'NewPart in Subassembly:',bolt
boltsketch = bolt.AddSketch('boltsketch',subassy.XYPlane)
print 'Sketchname:',boltsketch.Name
circle = boltsketch.AddCircle(0,0,10,False)
print 'added Circle:',circle
circlebody = bolt.AddExtrudeBoss('CircleBody',bolt.GetSketch('boltsketch'),40,False)
print 'created Body:',circlebody
headsketch = bolt.AddSketch('headsketch',subassy.XYPlane)
circleH = headsketch.AddCircle(0,0,15,False)
headbody = bolt.AddExtrudeBoss('HeadBody',bolt.GetSketch('headsketch'),10,True)

#bolt.AnchorPart()   HOW DO I DO THIS??

# Add washer to subassambly
washer = subassy.AddNewPart("Washer",0,0,0) # Assign the new part to the variable Bolt
print 'NewPart in Subassembly:',washer
washersketch = washer.AddSketch('washersketch',subassy.XYPlane)
print 'Sketchname:',washersketch.Name
circle1 = washersketch.AddCircle(0,0,11,False)
circle2 = washersketch.AddCircle(0,0,16,False)
print 'added Circle:',circle
circlebody = washer.AddExtrudeBoss('WasherBody',washer.GetSketch('washersketch'),3,False)
print 'created Body:',circlebody

# Add second washer
subassy.DuplicatePart(washer,0,0,27)

# Add nut to subassambly
nut = subassy.AddNewPart("Nut",0,0,30)
nutsketch = nut.AddSketch('nutsketch',nut.XYPlane)
hex = nutsketch.AddPolygon(0.0, 0.0, 16, 6, False)
hole = nutsketch.AddCircle(0,0,10,False)
nutbody = nut.AddExtrudeBoss('NutBody',nut.GetSketch('nutsketch'),6,False)

# You have to insert your save location instead of *******
subassy.Save(r"C:/Users/......")
loadedassy = Assembly(r"C:/Users/....../","BoltSet.ad_asm")

print loadedassy
sub_assy = ca.AddSubAssembly(loadedassy,0,0,0)
print sub_assy
win = Windows()
win.InfoDialog('Wait for save operation to be fullfilled...','Wait')
win.InfoDialog('...only to be sure before trying to close subassy and part window','Wait')

print 'close sub:',subassy
subassy.Close()
 

NateLiquidGravity

Alibre Super User
Apparently more needed to AddNewSubAssembly than I thought - but you must still look at what each functions/methods return.
 

Attachments

  • return.png
    return.png
    16.2 KB · Views: 8

NateLiquidGravity

Alibre Super User
Some things to be cleared up:
  • Some functions do not have a return (void) so there is no need to assign it to a variable. For example AddCircle and AddPolygon have no return, that's why the console says "added Circle: None":
  • If you don't plan on using a return value there is generally no need to assign it to a variable. For example if you aren't using the Feature object created by AddExtrudeBoss there is no need to assign it to a variable.
  • Conversely if you assign the returned sketch to a variable then you can use that variable later.
For example nutsketch is the variable assigned the sketch so:
Code:
nutbody = nut.AddExtrudeBoss('NutBody',nut.GetSketch('nutsketch'),6,False)
Becomes:
Code:
nutbody = nut.AddExtrudeBoss('NutBody',nutsketch,6,False)

  • Maybe Andy @ajayre can confirm but there seems to be no need to load the assembly from the saved file if it is already open for editing.
For example:
Code:
subassy.Save(r"C:/Users/......")
loadedassy = Assembly(r"C:/Users/....../","BoltSet.ad_asm")
sub_assy = ca.AddSubAssembly(loadedassy,0,0,0)
Becomes:
Code:
subassy.Save(r"C:/Users/......")
sub_assy = ca.AddSubAssembly(subassy,0,0,0)
 

idslk

Alibre Super User
Hello Nate,

thanks for your interest.
Some functions do not have a return (void)
correct
if you aren't using the Feature object
i don't know if it will be used
if you assign the returned sketch to a variable
correct

as the first script was a quickshot(only to start with), here a shortened one(tested):
Code:
ca = CurrentAssembly()
subassy = Assembly('BoltSet')
bolt = subassy.AddNewPart("Bolt",0,0,0)
boltsketch = bolt.AddSketch('boltsketch',subassy.XYPlane)
boltsketch.AddCircle(0,0,10,False)
circlebody = bolt.AddExtrudeBoss('CircleBody',boltsketch,20,False)
subassy.AnchorPart(bolt)
import tempfile
temporary = tempfile.gettempdir() + '\\'
subassy.Save(temporary)
sub_assy = ca.AddSubAssembly(subassy,0,0,0)
subassy.Close()

if there is something to be optimized, ideas are welcome;)

Regards
Stefan
 

albie0803

Alibre Super User
The temp file setting really threw me as I didn't think to actively find it and delete the generated files after my first run. Nothing was changing no matter what I did.
Saw that the temp File had a folder called Alibre Design in it so I set it to there and then found the following code to clear it out when the script is first run.

Code:
import os, shutil
import tempfile

folder = tempfile.gettempdir() + '\\Alibre Design\\'
for the_file in os.listdir(folder):
    file_path = os.path.join(folder, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception as e:
        print(e)

I will add a popup window to tell myself to save the assembly (to M-Files) after using it.

I have added a menu box at the start which lets you pick what sort of bolt/washer/nut setup you desire.

Next will be to add this to the bolt script and add dimensions for washers and nuts.
 

ajayre

Alibre Super User
I think it would be better to only delete files that your script creates. For all we know Alibre Design could be creating files in that folder that it needs.

You could create your own subfolder. Take a look at tempfile.mkdtemp().

Andy
 
The need to save the assembly before adding it as a subassembly - does seem like it should be optional.
Nate -- The difference between an Assembly and a Sub-Assembly (or Sub-Sub-Assembly) is one of organizational consideration and not (if you will) CAD File Structure. One example of this would be (say) a Pillow Block Bearing Assembly that is used as part of a Project's Sub-Assembly. As a "Purchased part," it is treated as a "Single Part" rather than as a "Sub-Sub-Assembly." Such distinctions are important from a Project Management point of view.
 

oldfox

Alibre Super User
Next will be to add this to the bolt script and add dimensions for washers and nuts.

Albie, I don't know the significance of this since I don't know what exactly you need concerning M-files.
If you are already using those scripts and still need the dimensions for something else, then just disregard this.:)
 

albie0803

Alibre Super User
@ajayre Going to bed I had the same thought. The Alibre folder was empty when I looked but nothing guarantees it will stay that way. I will definitely look into creating and deleting my own folder.

@oldfox M-Files just saves everything. If I add a part that is external to M-Files, M-Files will save a copy of it and will then use its internal copy from then on. ajayre says that scripting doesn't work with M-Files. If I have to save the subassembly before loading it into the parent assembly then I (think) I need to save (to M-Files) before I delete the subassembly files. I will have to check this out when I go back to work as I don't have M-Files at home.

As far as washers and nuts, I will simply add columns to your data table :)

As isdlk has mentioned earlier, if the saving of the subassembly is not essential then that would be great. The manual workflow as shown earlier suggests that Nate's earlier code should work, but it doesn't.

Code:
Assy = CurrentAssembly()
BoltSet = Assy.AddNewSubAssembly("BoltSet",0,0,0) # Assign the new subassembly to the variable BoltSet
Bolt = BoltSet.AddNewPart("Bolt",0,0,0) # Assign the new part to the variable Bolt
 
Last edited:
Top