What's new

Can I check if the file a script is running in is a part or assembly?

albie0803

Alibre Super User
Ok, I'm lazy, but I want to be able to just run a script and have the code check if the file is a part or assembly and reference it accordingly.

Is this possible?
 

albie0803

Alibre Super User
I actually thought about it for a while and looked it up dealing with exceptions as you were typing.
I came up with

Code:
try:
    Assy = CurrentAssembly()
    Screw = Assy.AddNewPart("Cap Screw M%dx%d" % (Diameter, Length), 0, 0, 0)
except:
    Screw = CurrentPart()

Thanks, appreciate it.
 
Last edited:

otrotabi

Member
Thanks for sharing the code. Could you please explain what it does ? It tells you whether or not the screw is part of th current assembly ? Regards.
 

albie0803

Alibre Super User
My intent is for the script to determine if it is running in an assembly, and if so, add a new part to the assembly or just create a part if being run from a part file'

so, how it works.

try:
Assy = CurrentAssembly()
Screw = Assy.AddNewPart("Cap Screw M%dx%d" % (Diameter, Length), 0, 0, 0)
except:
Screw = CurrentPart()

It tries to reference the current file as Assy.
If it can, (because you have an assembly open) it then creates a new part in the assembly called Screw.
If it can't, (because you have a part file open) it throws an exception which is caught by the except statement and the current file gets defined as a part called Screw and the script continues
 
Top