Assembly 4 scripting assembly using macro

Discussion about the development of the Assembly workbench.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
kamer73
Posts: 1
Joined: Fri Apr 22, 2022 12:36 pm

Assembly 4 scripting assembly using macro

Post by kamer73 »

Hello everybody!

First I'd like to thank a lot Zolko and other contributors for their work as it is quite amazing, I really enjoy the Assembly 4 module!

From using it I was wondering if it would be possible to automatize the creation of an assembly: I would like to run some code to calculate some stuff, optimize on position of those objects, write all of this in a csv, and then use a FreeCad macro to automatically generate the layout with pre-created pieces.

I think I would only need the macro to insert a part, and specify its position relatively to some LCS I would have inserted at different places.

From what says the python console, inserting a part is only done through the GUI interface: inserting a part gives me:

Code: Select all

>>> Gui.runCommand('Asm4_insertLink',0)
>>> # Gui.Selection.clearSelection()
>>> # Gui.Selection.removeSelection('tracker','Assembly','PlancheBas.')
I tried to read a bit the code on github but thought it would be preferable to previously ask where I should be looking.
I guess the objective should be to create a class insertLink, but I don't know if that's possible from command line.

Thanks a lot!
Simon
nic
Posts: 135
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Assembly 4 scripting assembly using macro

Post by nic »

kamer73 wrote: Fri Apr 22, 2022 1:04 pm
From using it I was wondering if it would be possible to automatize the creation of an assembly
Hello,

Im 'finding myself in the same situation ; any hint on this topic?

My goal is to automatize some assemblies based on csv/spreadsheet...
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Assembly 4 scripting assembly using macro

Post by Zolko »

nic wrote: Fri Sep 16, 2022 1:44 pm My goal is to automatize some assemblies based on csv/spreadsheet...
please do. I wouldn't know how to do that so I can't help you there
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Assembly 4 scripting assembly using macro

Post by onekk »

kamer73 wrote: Fri Apr 22, 2022 1:04 pm ...
Probably is not the best way, but mimincking what @Zolko has done in his command, you could create things, this is from:

https://github.com/Zolko-123/FreeCAD_As ... odelCmd.py


See maybe this test code:
20220920_Asm4test.py
(1.4 KiB) Downloaded 51 times
What Zolko as done in a very good way in respect to RealThunder branch is that Asm4 is an Addon, and some of his added things have been incorporated in the stock FC source base.

So adding an Asm4 object is not too difficult, as they are simply "expanded" standard FC objects.

But from this point on I could not help you anymore, as I don't use Assembly4.

Probably with some skeleton code is more easy to obtain help.

If you don't know what you are seeing in the Python Console, is an "echo" of the command issued, not what is done to create objects.

Code: Select all

Gui.runCommand('Asm4_insertLink',0)
And usually a command is supposed to use the Gui.

The code I've attached is using only:

Code: Select all

import FreeCAD

import Asm4_libs as Asm4
Probably the relevant part of code to study and replicate is contained in:


https://github.com/Zolko-123/FreeCAD_As ... PartCmd.py

Hope it helps.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
nic
Posts: 135
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Assembly 4 scripting assembly using macro

Post by nic »

thank you,
I'm on the way to achieve my goal (something like putting an array of variant links with a set of variables changes and Placements). I will put the macro here once "finished" (between quotes since it only fits my needs for now).

@Zolko , would you be open to some pull requests? I think it would be desirable to separate GUI stuff from Asm4 backend, such as we could create scripted objects without ugly copy/paste?
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Assembly 4 scripting assembly using macro

Post by Zolko »

nic wrote: Tue Sep 20, 2022 6:49 am @Zolko , would you be open to some pull requests?
Yes. If they follow the général Asm4 feeling
try the Assembly4 workbench for FreCAD — tutorials here and here
nic
Posts: 135
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Assembly 4 scripting assembly using macro

Post by nic »

nic wrote: Tue Sep 20, 2022 6:49 am I'm on the way to achieve my goal (something like putting an array of variant links with a set of variables changes and Placements).
Here is a (somewhat limited) macro that fits my needs right now:
variant-link_batch-import.FCMacro
(5.54 KiB) Downloaded 50 times
The macro needs two path variables (defined at the top of the macro, lines 20 and 21 of the current version):

Code: Select all

# ==============================================================================
# Data for VariantLink batch assemby
#
PARTPATH = "~/numeric-shared/beam_assy/beamassy_wdir/111W6301/111W6301.FCStd"
CSVPATH = "~/databeam.csv"
# ==============================================================================
One triggered, the macro will open the part in the current FreeCAD session, and parse the CSV data file to create variant links as per.

The csv file needs four mandatory columns: "linkName", "attachOffsetBase", "attachedBy", and "attachedTo". Additional columns are processed as Asm4 Variables names.

Example of csv (tab-separated) file:

Code: Select all

A_length	linkName	angle_thickness	angle_width	attachOffsetBase	attachedBy	attachedTo
4.26	L_angle_var	0.125	0.75	(18.86, 0.0, 0.0)	#LCS_1	FWD_WEB#LCS_ref
4.32	L_angle_var_2	0.125	0.75	(26.24, 0.0, 0.0)	#LCS_1	FWD_WEB#LCS_ref
4.43	L_angle_var_3	0.125	0.75	(33.62, 0.0, 0.0)	#LCS_1	FWD_WEB#LCS_ref
4.61	L_angle_var_4	0.125	0.75	(41.0, 0.0, 0.0)	#LCS_1	FWD_WEB#LCS_ref
The macro defines a "makeSciptedVariantLink" class, allowing the creation of VariantLink from command line (no gui). Its code is mainly copy/paste from different Asm4 source files.

A function ("batchAssemble") will the create one instance of this class using data from the CSV file.

------

To me, the next steps should be to modify Asm4 to avoid code duplication (copy/paste) like I did. Maybe a common Ancestor handling the core, from who the "Asm4.variantLinkCmd.makeVariantLink" could inheritate, keeping only gui stuff for it...
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Assembly 4 scripting assembly using macro

Post by Zolko »

nic wrote: Mon Sep 26, 2022 6:05 am To me, the next steps should be to modify Asm4
what do you mean ?
try the Assembly4 workbench for FreCAD — tutorials here and here
nic
Posts: 135
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Assembly 4 scripting assembly using macro

Post by nic »

I mean, if you check the macro above, the created class has a lot (almost all of its) code copied from variantLinkCmd.makeVariantLink, but expurged from graphical stuff. It would be a pitty to duplicate this code (readability, maintenability, etc.).
So, if we could cherry pick and isolate the required block of codes from your class, we could more easily create some automated stuff.

Asm4 is a great design, opening the door to automated builds of parametric assemblies. We just need to isolate the core functionalities from the graphical commands. One of the example is the Activated() method, where I needed to copy 80% of its code at different locations...

I hope it's clear...
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Assembly 4 scripting assembly using macro

Post by adrianinsaval »

why not use the built-in configuration table functionality [1] and just iterate through the configuration enum property with a macro?

[1] https://wiki.freecadweb.org/Spreadsheet ... ion_tables
Post Reply