What's new

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

albie0803

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

I have wrestled with the code (and google) and come up with the following that checks if a temp directory exists, if not, creates it and if so deletes the contents of it.

Code:
import tempfile
import os

# define temp folder
tmp = tempfile.gettempdir() + '\\alibre\\'
# if no temp folder create it
if os.path.isdir(tmp) == False:
  os.makedirs(tmp)
else:
  #delete file in temp folder
  for the_file in os.listdir(tmp):
    file_path = os.path.join(tmp, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception as e:
        print(e)

This will go at the beginning of the script.
 

idslk

Alibre Super User
Hello colleagues,

had somebody of you tested the "DistanceTo()" function in the Beta?
Will we get a correct distance between eg. two flange faces?

Regards
Stefan
 

NateLiquidGravity

Alibre Super User
The version of Alibre Script packed in the current Alibre Design beta is already obsolete - so I was waiting until the newest version is out to really test things.
 

NateLiquidGravity

Alibre Super User
The Alibre API says:
To save to MFiles vaults using the API, you will use the same methods as with the file system. The filepath should be passed with the following format: [MFiles drive letter]:\[Vault name] (ie. "M:\MyVault")
If Alibre Design treats it the same as the filesystem, then perhaps maybe we are overthinking it? Just save your temp files to the vault with TEMP (or something) appended to their name?
 

albie0803

Alibre Super User
I have the bolt/washer/washer/nut subassembly being created ok with the parts arriving at 0,0,0. I'm now working on setting up constraints to assemble the set as required.
The bolt is mandatory (of course) but the rest are optional.

Next Problem:

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

subassy.AddMateConstraint(0, Washer, Washer.GetPlane("BoltPlane"),  subassy, subassy.YZPlane)

TypeError: expected IInstance, got Assembly

Code:
subassy.AddMateConstraint(0, Washer, Washer.GetPlane("BoltPlane"),  BoltSet, BoltSet.YZPlane)

NameError: name 'BoltSet' is not defined

I'm trying to mate the part Washer using BoltPlane to the YZPlane of the assembly BoltSet (referred to as subassy)
 

idslk

Alibre Super User
Hello albie0803,

tried it and got an "interesting" result...
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',bolt.GetSketch('boltsketch'),40,False)
headsketch = bolt.AddSketch('headsketch',subassy.XYPlane)
circleH = headsketch.AddCircle(0,0,15,False)
headbody = bolt.AddExtrudeBoss('HeadBody',bolt.GetSketch('headsketch'),10,True)
subassy.AnchorPart(bolt)
washer = subassy.AddNewPart("Washer",0,0,0) # Assign the new part to the variable Bolt
washersketch = washer.AddSketch('washersketch',subassy.XYPlane)
washersketch.AddCircle(0,0,11,False)
washersketch.AddCircle(0,0,16,False)
washerbody = washer.AddExtrudeBoss('WasherBody',washer.GetSketch('washersketch'),3,False)
subassy.DuplicatePart(washer,0,0,27)
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)
import tempfile
temporary = tempfile.gettempdir() + '\\'
subassy.Save(temporary)
sub_assy = ca.AddSubAssembly(subassy,0,0,0)
lf_p = ca.SubAssemblies[0].Parts[1]
lf = ca.SubAssemblies[0].Parts[1].GetFaces()
bf_p = ca.SubAssemblies[0].Parts[0]
bf = ca.SubAssemblies[0].Parts[0].GetFaces()
print 'Mate partners',bf_p,bf[0],' + ',lf_p,lf[3]
ca.AddMateConstraint(0, lf_p, lf[3], bf_p, bf[0])
print 'close sub:',subassy
subassy.Close()

"New" code begins 8 lines before end.
The script will complain the constraint. click close (ignore it...) the script will correctly end.
Try to edit the script-created contraint and compare the faces in the design explorer.
Try to add another contraint.
Supress the constraint and add a new constraint then...
Can't explain the result...
Maybe someone has a better idea.

Regards
Stefan
 
Last edited:

ajayre

Alibre Super User
I have the bolt/washer/washer/nut subassembly being created ok with the parts arriving at 0,0,0. I'm now working on setting up constraints to assemble the set as required.
The bolt is mandatory (of course) but the rest are optional.

Next Problem:

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

subassy.AddMateConstraint(0, Washer, Washer.GetPlane("BoltPlane"),  subassy, subassy.YZPlane)

TypeError: expected IInstance, got Assembly

Code:
subassy.AddMateConstraint(0, Washer, Washer.GetPlane("BoltPlane"),  BoltSet, BoltSet.YZPlane)

NameError: name 'BoltSet' is not defined

I'm trying to mate the part Washer using BoltPlane to the YZPlane of the assembly BoltSet (referred to as subassy)

You can only add constraints to parts and assemblies that have been added to the assembly. When you add a subassembly to an assembly the function returns an AssembledSubAssembly object. When you add a part to an assembly the function returns an AssembledPart object. Add constraints between these AssembledXXX objects.

Andy
 

idslk

Alibre Super User
@ajayre,

So opened a new assembly, let the script(which creates a subassembly)run , script ends -- everything looks fine.
(no code for automatic mate!)
Then i tried to constraint the script created subassembly manualy -- no luck!
Why?

Regards
Stefan
 

idslk

Alibre Super User
Hello Andy,
Then i tried to constraint the script created subassembly manualy -- no luck!
i reintegrated the following code
Code:
loadedassy = Assembly(r"C:/Users/....../","BoltSet.ad_asm")
sub_assy = ca.AddSubAssembly(loadedassy,0,0,0)
played it again and then i've been able to add constraints manually.
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.
It seems that we can't disclaim loading the subassembly again after creating?

Regards
Stefan
 

ajayre

Alibre Super User
@ajayre,

So opened a new assembly, let the script(which creates a subassembly)run , script ends -- everything looks fine.
(no code for automatic mate!)
Then i tried to constraint the script created subassembly manualy -- no luck!
Why?

Regards
Stefan

Sorry if I am misunderstanding but if you are seeing a problem outside of Script, even if the assembly was created with a script, then probably best to submit the problem to support along with the assembly files and part files.

Andy
 

idslk

Alibre Super User
Hello Andy,
thanks for your response
Sorry if I am misunderstanding
you're right. It is a not a script problem in effect.
I found a way to establish an constraint in the 'boltset' subassembly:D
from within the main script.
To see the constraint you have to edit the Boltset (eg. Edit here)
upload_2019-6-17_16-45-7.png
Code:
import time
print 'Start'
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,40,False)
headsketch = bolt.AddSketch('headsketch',subassy.XYPlane)
headsketch.AddCircle(0,0,15,False)
headbody = bolt.AddExtrudeBoss('HeadBody',headsketch,10,True)
subassy.AnchorPart(bolt)
washer = subassy.AddNewPart("Washer",0,0,0)
washersketch = washer.AddSketch('washersketch',subassy.XYPlane)
washersketch.AddCircle(0,0,11,False)
washersketch.AddCircle(0,0,16,False)
washerbody = washer.AddExtrudeBoss('WasherBody',washer.GetSketch('washersketch'),3,False)
subassy.DuplicatePart(washer,0,0,27)
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)
sa_p = subassy.Parts
lf = sa_p[1].GetFaces()
bf = sa_p[0].GetFaces()
print 'Mate partners in subassembly',bf[0],sa_p[0],' + ',lf[3],sa_p[1]
time.sleep(1)
subassy.AddMateConstraint(0, sa_p[1], lf[3], sa_p[0], bf[0])
import tempfile
temporary = tempfile.gettempdir() + '\\'
subassy.Save(temporary)
print 'close constrained subassembly',subassy.Name
subassy.Close()
print 'Load constrained subassembly'
loadedassy = Assembly(temporary,"BoltSet.ad_asm")
sub_assy = ca.AddSubAssembly(loadedassy,0,0,0)
print 'close loaded subassembly',loadedassy
loadedassy.Close()
Now it's time for optimizers...

Regards
Stefan
 

NateLiquidGravity

Alibre Super User
If Alibre Design treats it the same as the filesystem, then perhaps maybe we are overthinking it? Just save your temp files to the vault with TEMP (or something) appended to their name?
Wel, l I tested and this doesn't work for me. I get this error when trying 'M:\Alibre' for the save path.
Code:
System.Runtime.InteropServices.COMException (0x80040270): Invalid null reference.
   at com.alibre.automation.ExceptionMap.handleException(Exception inputException)
   at com.alibre.automation.AlibreSession.SaveAs(Object& destination, String itemName)
   at AlibreScript.API.Assembly.SaveAs(String Folder, String NewName)
   at Microsoft.Scripting.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at AlibreScript.UI.IronTextBoxControl.#ijb(Object #9A)
 

albie0803

Alibre Super User
In light of your comment above

ca = CurrentAssembly()
sub_assy = ca.AddSubAssembly(subassy,0,0,0)

Code:
# Select a face to mate to
win = Windows()
options = []
values = []
own_face = None
options.append(['Select a face' , WindowsInputTypes.Face , None])
values = win.OptionsDialog( 'Mate Bolt to which Face', options , 180 )
own_face = values[0]
P1 = own_face.GetPart()

ca.AddMateConstraint(0, sub_assy, sub_assy.XYPlane, P1, own_face)

returns the error: SystemError: DesignSession(sessionID=0)

Getting this mate to work so that the subassembly is not lost inside the existing assembly is the final hurdle in my project (I hope :rolleyes:)
 

NateLiquidGravity

Alibre Super User
Are you using the beta?

Won't work prior to beta:
Face.GetPart() returns a Part

Will work in the beta:
Face.GetPart() returns an AssembledPart
 
Top