can I work "iteratively" with the openscad workbench?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
oren_daniel
Posts: 2
Joined: Sun Jan 17, 2021 8:43 pm

can I work "iteratively" with the openscad workbench?

Postby oren_daniel » Thu Feb 25, 2021 9:52 pm

Hello,
What I am looking for might not be possible, but I will ask anyway.

I want to use Openscad inside of Freecad, the problem is that the Openscad element button, allows me to enter scad code once, and once executed I can't modify what I have written (because Freecad will convert my code onto primitives such as a cube). So if I want to change something in my script, I need to delete everything and start again.
I have also tried to write my script in Openscad itself and importing to freecad, however it still doesn't achieve what I want because if I will change my .scad file it won't effect my imported freecad file.

What I am looking for Ideally is to have a sort of a "SCad object" which will contain my Openscad script, rather then directly executing and converting my scad script to freecad primitives, and if I want to change something in my shape I just change the script, and recompile it (as if I am working on a regular openscad session).

Is there an external workbench which will achieve this, or can I script this behavior?

Thank for the help!
keithsloan52
Posts: 1859
Joined: Mon Feb 27, 2012 5:31 pm

Re: can I work "iteratively" with the openscad workbench?

Postby keithsloan52 » Sat Feb 27, 2021 6:03 am

oren_daniel wrote: Thu Feb 25, 2021 9:52 pm Hello,
What I am looking for might not be possible, but I will ask anyway.

I want to use Openscad inside of Freecad, the problem is that the Openscad element button, allows me to enter scad code once, and once executed I can't modify what I have written (because Freecad will convert my code onto primitives such as a cube). So if I want to change something in my script, I need to delete everything and start again.
I have also tried to write my script in Openscad itself and importing to freecad, however it still doesn't achieve what I want because if I will change my .scad file it won't effect my imported freecad file.

What I am looking for Ideally is to have a sort of a "SCad object" which will contain my Openscad script, rather then directly executing and converting my scad script to freecad primitives, and if I want to change something in my shape I just change the script, and recompile it (as if I am working on a regular openscad session).

Is there an external workbench which will achieve this, or can I script this behavior?

Thank for the help!
Unfortunately I currently have too much on my plate but I think there may be a nice way forward using Jupyter. You can setup Jupyter to interact with FreeCAD, so it should be possible to get it to do the extra step of take OpenSCAD input and invoke the FreeCAD OpenSCAD importer.

@RealThunder branch also supports Jupyter, it would do no harm to ask him about implementing my suggestion, would probably help if you made a small donation to his Patreon https://www.patreon.com/thundereal
realthunder
Posts: 1963
Joined: Tue Jan 03, 2017 10:55 am

Re: can I work "iteratively" with the openscad workbench?

Postby realthunder » Sat Feb 27, 2021 12:48 pm

keithsloan52 wrote: Sat Feb 27, 2021 6:03 am @RealThunder branch also supports Jupyter, it would do no harm to ask him about implementing my suggestion, would probably help if you made a small donation to his Patreon https://www.patreon.com/thundereal
Looks like Jupyter is going to be popular among FreeCAD users.

oren_daniel wrote: Thu Feb 25, 2021 9:52 pm Hello,
What I am looking for might not be possible, but I will ask anyway.
Hi, if you have never heard of Jupyter Notebook, please search around. Here is how you can start Jupyter using my release. Assuming you are using Windows, download and extract the 7z file. Use CMD to go inside the bin directory, and start Jupyter with the following command,

Code: Select all

python -m jupyter notebook
You browser will launch with the following looking page. Select 'FreeCAD' to create a new notebook as shown below. Paste in the following code, and press SHIFT + Enter. FreeCAD will now launch (be patient, it may take a while), and you shall see the result of the OpenSCAD model. Modify the OpenSCAD model in the notebook, press SHIFT + Enter again to see the update. That's it.
Screenshot from 2021-02-02 21-06-19.png
Screenshot from 2021-02-02 21-06-19.png (11.05 KiB) Viewed 551 times

Code: Select all

%init_freecad gui
scadstr='''
module example_intersection()
{
	intersection() {
		difference() {
			union() {
				cube([30, 30, 30], center = true);
				translate([0, 0, -25])
					cube([15, 15, 50], center = true);
			}
			union() {
				cube([50, 10, 10], center = true);
				cube([10, 50, 10], center = true);
				cube([10, 10, 50], center = true);
			}
		}
		translate([0, 0, 5])
			cylinder(h = 50, r1 = 20, r2 = 5, center = true);
	}
}

example_intersection();
'''
import OpenSCADUtils, importCSG
doc=FreeCAD.activeDocument() or FreeCAD.newDocument()
obj = doc.getObject('OpenSCADPart')
if obj:
    for o in App.getDependentObjects(obj.Group, 1):
        doc.removeObject(o.Name)
    doc.removeObject(obj.Name)
# covert the above OpenSCAD script into CSG script
tmpfilename=OpenSCADUtils.callopenscadstring(scadstr,'csg')
try:
    # remember the existing objects
    objset = set(doc.Objects)
    # import CSG file
    importCSG.insert(tmpfilename, doc.Name)
    # find all new objects
    objs = [obj for obj in doc.Objects if obj not in objset]
    # create a part contain to hold new objects
    group = doc.addObject('App::Part', 'OpenSCADPart')
    group.Group = objs
    # recompute and adjust the view to include the newly created objects
    doc.recompute()
    Gui.Selection.addSelection(group)
    Gui.runCommand('Std_ViewSelectionExtend')
    Gui.Selection.clearSelection()
finally:
    os.unlink(tmpfilename)
Try Assembly3 (latest version 0.11) along with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
randfb1
Posts: 11
Joined: Mon Dec 30, 2013 5:44 pm

Re: can I work "iteratively" with the openscad workbench?

Postby randfb1 » Sun Apr 04, 2021 12:20 am

realthunder, thanks for your excellent work on this version.

I am attempting to follow your instructions on using freecadLink with Jupyter.

When I enter your specified comand,
I get the error:

C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin>python -m jupyter notebook
Fatal error in launcher: Unable to create process using '"c:\works\sw\freecadmakeimage\build\libpack15\bin\python.exe" "C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin\Scripts\jupyter-notebook.EXE" '

C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin>

Please advise,
Thanks,
Rand Batchelder
keithsloan52
Posts: 1859
Joined: Mon Feb 27, 2012 5:31 pm

Re: can I work "iteratively" with the openscad workbench?

Postby keithsloan52 » Sun Apr 04, 2021 7:53 am

randfb1 wrote: Sun Apr 04, 2021 12:20 am realthunder, thanks for your excellent work on this version.

I am attempting to follow your instructions on using freecadLink with Jupyter.

When I enter your specified comand,
I get the error:

C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin>python -m jupyter notebook
Fatal error in launcher: Unable to create process using '"c:\works\sw\freecadmakeimage\build\libpack15\bin\python.exe" "C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin\Scripts\jupyter-notebook.EXE" '

C:\freecadLink\FreeCAD-asm3-Daily-Win64-Py3-Qt5-20210311\bin>

Please advise,
Thanks,
Rand Batchelder
Following realthunders work. I also looked at the same approach with FreeCAD GSOC project https://github.com/kryptokommunist/Jupyter_FreeCAD

I added info to a fork to get it roughly working on a Mac with OpenSCAD see https://github.com/KeithSloan/Jupyter_FreeCAD
I made a pull request but it has not been merged, so you would need to take a copy from my fork. @kryptokommunist

On my system the fitting of the display window is not totally satisfactory, but maybe that is down to my lack of Jupyter knowledge.
The biggest weakness I found is that if you have an error in your code, for example a syntax error the system is not
very friendly in terms of tying up with the source in Jupyter.

It should not be too hard to change what I did to work on Windows.
Last edited by keithsloan52 on Sun Apr 04, 2021 6:11 pm, edited 1 time in total.
realthunder
Posts: 1963
Joined: Tue Jan 03, 2017 10:55 am

Re: can I work "iteratively" with the openscad workbench?

Postby realthunder » Sun Apr 04, 2021 10:15 am

randfb1 wrote: Sun Apr 04, 2021 12:20 am realthunder, thanks for your excellent work on this version.

I am attempting to follow your instructions on using freecadLink with Jupyter.

When I enter your specified comand,
I get the error:
Please download the latest release image. The hard coded Python problem in windows release has already been fixed.
Try Assembly3 (latest version 0.11) along with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
keithsloan52
Posts: 1859
Joined: Mon Feb 27, 2012 5:31 pm

Re: can I work "iteratively" with the openscad workbench?

Postby keithsloan52 » Sun Apr 25, 2021 2:23 pm

oren_daniel wrote: Thu Feb 25, 2021 9:52 pm Hello,
What I am looking for might not be possible, but I will ask anyway.

I want to use Openscad inside of Freecad, the problem is that the Openscad element button, allows me to enter scad code once, and once executed I can't modify what I have written (because Freecad will convert my code onto primitives such as a cube). So if I want to change something in my script, I need to delete everything and start again.
I have also tried to write my script in Openscad itself and importing to freecad, however it still doesn't achieve what I want because if I will change my .scad file it won't effect my imported freecad file.

What I am looking for Ideally is to have a sort of a "SCad object" which will contain my Openscad script, rather then directly executing and converting my scad script to freecad primitives, and if I want to change something in my shape I just change the script, and recompile it (as if I am working on a regular openscad session).

Is there an external workbench which will achieve this, or can I script this behavior?

Thank for the help!
Have updated OpenSCAD add element to support
  • Load from file
  • Save to file
  • Refresh - Updates FreeCAD executing current source
Note: If you enter invalid OpenSCAD requests they will be reported in Report View, maybe a future enhancement will provide a better option.

Pull request https://github.com/FreeCAD/FreeCAD/pull/4764
Last edited by keithsloan52 on Wed Apr 28, 2021 8:09 am, edited 2 times in total.
User avatar
chennes
Posts: 862
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: can I work "iteratively" with the openscad workbench?

Postby chennes » Sun Apr 25, 2021 4:38 pm

keithsloan52 wrote: Sun Apr 25, 2021 2:23 pm
  • Load from file
  • Save to file
  • Refresh - Updates FreeCAD executing current source
Very nice!! I'm looking forward to giving this a try this week.
Chris Hennes
Pioneer Library System
keithsloan52
Posts: 1859
Joined: Mon Feb 27, 2012 5:31 pm

Re: can I work "iteratively" with the openscad workbench?

Postby keithsloan52 » Sun Apr 25, 2021 7:44 pm

chennes wrote: Sun Apr 25, 2021 4:38 pm
keithsloan52 wrote: Sun Apr 25, 2021 2:23 pm
  • Load from file
  • Save to file
  • Refresh - Updates FreeCAD executing current source
Very nice!! I'm looking forward to giving this a try this week.
Have added another Text Area for output of any errors i.e. syntax etc.
Will wait for feedback before making another Pull Request
OpenSCADCommands.py
(26.43 KiB) Downloaded 1 time