How to use expressions as in OpenScad

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!
User avatar
g0erwt
Posts: 11
Joined: Mon Jul 19, 2021 3:45 pm
Contact:

Re: How to use expressions as in OpenScad

Post by g0erwt »

@chennes ok, might be a problem in rendering, but that is not the real problem I was asking for help.
The real problem is that in the output of the FreeCAD OpenSCAD-Workbench everything, apart from the pieces left and upper, yellow (2x), oker (2x), grey (1x) and blue (1x) are not generated. In other words 6 pieces are correct, 4 pieces are wrong.

Question 1: is this due to the generated geometry (wire is the term used in FreeCAD) or is it something else?
Question 2: why is the intended "green" piece not exactly as it should be.
Question 3: why is there a triangle in "white". In stead of "white" I can understand it has "no colour" indication. But ... that triangle is not defined by a polygon. It looks like a "left-over".
Question 4: the correct pieces are indeed triangles; the missing ones have four (4) lines/points. Is that a better indication where to look?

keith sent me some info about the py-modules. I will have a look at that as well.

If this problem can not be solved I can not proceed with working with a model generated by real OpenSCAD. That would be a pity.

Thanks so far for your help.
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: How to use expressions as in OpenScad

Post by keithsloan52 »

I loaded the file in OpenSCAD and exported as CSG
Ruhig_Blut-reconstructie.csg
(1.67 KiB) Downloaded 8 times
The commands are of the form

Code: Select all

color([1, 1, 0, 1]) {
	polygon(points = [[-30, 51.9615], [-30, 69.282], [0, 69.282]], paths = [[0, 1, 2]], convexity = 10);
}
If one cuts and pastes them in turn into FreeCAD OpenSCAD AddElements you can see that a number of shapes overlap
and that is the main problem.

One of the shapes in OpenSCAD is green but not so in FreeCAD so there is probably a bug related to color but I don't think there
is anything much can be done about the overlapping shapes, other than to avoid them with the OpenSCAD programming.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: How to use expressions as in OpenScad

Post by TheMarkster »

You should probably invest the time to learn python scripting in FreeCAD. I make a beginning to converting your script to FreeCAD python, but it is not complete. I'm not sure how to color the faces in python. To use the script create a new macro in the Macros menu and copy/paste this into it, then run the macro.

Code: Select all

#// Ruhig Blut is een legpuzzel van de firma F. AD. Richter & Cie. uit Rudolstadt (Duitsland).
#// Het spel is omstreeks 1910 ontwikkeld.
#// Het bestaat uit twee delen. Een blok bevat 3 stenen. Het andere 7 stenen; analoog aan Tangram.
#//
#// Deze tekst beschrijft hoe de stenen samen in een doosje passen. Visueel door alle stenen een andere kleur te geven.
#// Echter ook de juiste wiskundige beschrijving wordt hierbij weergegeven.
#// De afmetingen kunnen m.b.v. één parameter worden gedefinieerd voor elke willekeurige grootte.
#// Het linkerblok plaatsen we in de het kwadrant links van de y-as.
#
#// De mogelijkheid om met Atom te werken als externe editor is geactiveerd.
#// D.w.z. dat in het vervolg Atom automatisch wordt geopend als OpenScad een model opent
#
#// Willen we alle coördinaten kunnen bepalen, dan is het zinnig die te benoemen.
#
#// Alle punten worden (eenmalig) genummerd. Per steen wordt telkens 'clockwise' genummerd.
#// Eerder gebruikte punten worden niet opnieuw genummerd.
#
#// definieer de parameter voor het automatisch bepalen van de grootte van het model.
#// Oorspronkelijk was deze waarde basis; we noemen die de basis
from math import sqrt
import Part
basis = 30;

def makePoint(x,y):
    p = Part.Point()
    p.X = x
    p.Y = y
    #p.Z = 0
    return p.toShape()

def extrude(face,len,name):
    ext = App.ActiveDocument.addObject("Part::Extrusion","Extrude")
    ext.Base = App.ActiveDocument.getObject(name)
    ext.LengthFwd = len
    ext.Solid = True
#
#// Linkerblok, in het negatieve x- kwadrant
#p0 = [0,0];
p0 = makePoint(0,0)
print("This is the origin of the x-y plane,  p0=", p0);
#// de oorsrong wordt apart genummerd
#p1 = [0,0];
p1 = makePoint(0,0)
#p2 = [-basis,0];
p2 = makePoint(-basis,0)
#p3 = [-basis,basis*sqrt(3)];
p3 = makePoint(-basis,basis*sqrt(3))
#p4 = [-basis,basis*4/sqrt(3)];
p4 = makePoint(-basis,basis*4/sqrt(3))

#p5 = [0,basis*4/sqrt(3)];
p5 = makePoint(0,basis*4/sqrt(3))
#// Print de resultaten
#
print("p1=",p1);
print("p2=",p2);
print("p3=",p3);
print("p4=",p4);
print("p5=",p5);
#
#// Blok twee bevat de punten in het positieve kwadrant van de x-as
#// p1 = [0,0];
p6 = [0,basis*sqrt(3)];
p7 = [basis,basis*sqrt(3)];
p8 = [basis,basis*4/sqrt(3)];
p9 = [basis*2,basis*4/sqrt(3)];
p10 = [basis*2,basis*4/sqrt(3)];
p11 = [basis*1.5,basis*0.5*sqrt(3)];
p12 = [basis*0.5,basis*0.5*sqrt(3)];
p13 = [basis,basis*sqrt(3)];
p14 = [basis*2,0];
p15 = [basis,0];
#// Print de resultaten
print("p1=",p1);
print("p6=",p6);
print("p7=",p7);
print("p8=",p8);
print("p9=",p9);
print("p10=",p10);
print("p11=",p11);
print("p12=",p12);
print("p13=",p13);
print("p14=",p14);
print("p15=",p15);
#
#// Print de resultaten
#
#
#
#color()
#polygon(points=[[0,0],[-basis,0],[-basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
pol0 = Part.makePolygon([p1.Point, p2.Point, p3.Point, p1.Point])
Part.show(pol0,"polygon0")

extrude(pol0,10,"polygon0")


#color("yellow")
#polygon(points=[[-basis,basis*sqrt(3)],[-basis,basis*4/sqrt(3)],[0,basis*4/sqrt(3)]], paths=[[0,1,2]],convexity=10);
pol1 = Part.makePolygon([p2.Point, p3.Point, p4.Point, p2.Point])
Part.show(pol1,"polygon1")
extrude(pol1, 10, "polygon1")
#color("gray")
#polygon(points=[[0,basis*4/sqrt(3)],[0,0],[-basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
pol2 = Part.makePolygon([p3.Point, p4.Point, p5.Point, p3.Point])
Part.show(pol2,"polygon2")
extrude(pol2, 10, "polygon2")
#
#
#
#
#color()
#polygon(points=[[0,0],[0,basis*sqrt(3)],[basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
#//mirror ([0,0,0])
#
#//creeër een tussenfiguur van dezelfde vorm, maar gespiegeld om de as [basis,0,0] [basis,basis*2,0]
#// color wit
#color("white")
#polygon(points=[[basis*2,0],[basis*2,basis*sqrt(3)],[basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
#
#//creeër nu het trapezium, waarbij de snijpunten op de zijden van de ontstane 'open' driehoek zichtbaar worden
#color("magenta")
#polygon(points=[[0,0],[basis*0.5,basis*0.5*sqrt(3)],[basis*1.5,basis*0.5*sqrt(3)],[basis*2,0]], paths=[[0,1,2,3]],convexity=10);
#// bepaal nu het middelste stuk door de lijn te trekken van [basis*0.5,basis*0.5*sqrt(3)] naar [basis*2,0] met een overlappende kleur
#color("blue")
#polygon(points=[[0,0],[basis*0.5,basis*0.5*sqrt(3)],[basis*2,0]], paths=[[0,1,2]],convexity=10);
#// hiermee wordt zichtbaar dat het snijpunt van de 'druppels' in het midden resp. linksonder op een x-as van basis liggen.
#// langs de (y-as) loopt de lijn vanuit het midden = [[basis, 0],[basis, basis*sqrt(3)]]
#// het snijpunt is dan [basis, basis/sqrt(3)]
#// kappen daar de blauwedroek dus af dan komt de andere 'druppel' te voorschijn.
#color("green")
#polygon(points=[[0,0],[basis*0.5,basis*0.5*sqrt(3)],[basis,basis/sqrt(3)],[basis,0]], paths=[[0,1,2,3]],convexity=10);
#
#// nu zijn de hoekpunten van de druppel in het midden ook te bepalen, omdat die symmetrisch liggen t.o.v. de lijn [[basis,0],[basis,basis*sqrt(3)]]
#color("pink")
#polygon(points=[[basis*0.5,basis*0.5*sqrt(3)],[basis,basis*sqrt(3)],[basis*1.5,basis*0.5*sqrt(3)],[basis,basis/sqrt(3)]], paths=[[0,1,2,3]],convexity=10);
#// nu moet alleen nog de verbindingslijn tussen de twee stukken rechts bepaald worden
#// de lijn vanuit [0,0] via [basis*1.5,basis*0.5*sqrt(3)] moet worden doorgetrokken naar de buitenzijde [basis*2,basis*2]
#// dit is weer een driehoek met een hoek van 30 en 60 graden, waarvan de basis basis*2 is.
#// dus is rechtopstaande deel [0,basis*2/sqrt(3)]
#color("red")
#polygon(points=[[basis,0],[basis,basis/sqrt(3)],[basis*2,basis*2/sqrt(3)],[basis*2,0]], paths=[[0,1,2,3]],convexity=10);
#
#// nu kan de figuur worden afgemaakt door de bovenste driehoek in te passen.
#//color("blue")
#//polygon(points=[[0,basis*sqrt(3)],[0,basis*2],[basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
#
#
#// nu het bovendeel, dat dus basis*2 breed is
#color("blue")
#polygon(points=[[0,basis*4/sqrt(3)],[basis*2,basis*4/sqrt(3)],[basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
#
#// het linkerboven stukje
#color("yellow")
#polygon(points=[[0,basis*sqrt(3)],[0,basis*4/sqrt(3)],[basis,basis*sqrt(3)]], paths=[[0,1,2]],convexity=10);
#// bij de volgende blokken wordt het iets moeilijker om de coordinaten te bepalen
#//color ()
#// de vier overgebleven stukken zijn twee aan twee identiek, lijkt me
#// dat betekent dat de maat basis weer leidend is.
#//polygon(points=[[52,0],[77,basis*0.5*sqrt(3)],[102,basis/sqrt(3)],[102,0]], paths=[[0,1,2,3]],convexity=10);
#// in principe zouden we nu moeten kunnen spiegelen om de lijn [77,basis*0.5*sqrt(3)],[102,basis/sqrt(3)]
#// dat zoeken we later wel uit
#// nu het andere blok toevoegen
#//color("green")
#//polygon(points=[[102,basis/sqrt(3)],[77,basis*0.5*sqrt(3)],[102,basis*sqrt(3)],[127,basis*0.5*sqrt(3)]], paths=[[0,1,2,3]],convexity=10);
#// alleen de twee laatste stukken non; in purple
#color("purple")
#polygon(points=[[basis,basis*sqrt(3)],[basis*2,basis*4/sqrt(3)],[basis*2,basis*2/sqrt(3)],[basis*1.5,basis*0.5*sqrt(3)]], paths=[[0,1,2,3]],convexity=10);
#//color()
#//polygon(points=[[102,basis*sqrt(3)],[152,basis*2],[152,basis*2/sqrt(3)],[127,basis*0.5*sqrt(3)]], paths=[[0,1,2,3]],convexity=10);
#// bijna goed; het snijpunt van groen, purple en oker is nog niet in orde

App.ActiveDocument.recompute()
Gui.SendMsgToActiveView("ViewFit")
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: How to use expressions as in OpenScad

Post by keithsloan52 »

TheMarkster wrote: Thu Jul 22, 2021 4:35 pm You should probably invest the time to learn python scripting in FreeCAD. I make a beginning to converting your script to FreeCAD python, but it is not complete. I'm not sure how to color the faces in python. To use the script create a new macro in the Macros menu and copy/paste this into it, then run the macro.
To colour a face you have to set the color in the Objects ViewProvider.

Also programming in a FreeCAD python script is NOT going to solve the problem if the shapes overlap, the only way is to avoid overlapping shapes.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: How to use expressions as in OpenScad

Post by TheMarkster »

keithsloan52 wrote: Thu Jul 22, 2021 4:38 pm
TheMarkster wrote: Thu Jul 22, 2021 4:35 pm You should probably invest the time to learn python scripting in FreeCAD. I make a beginning to converting your script to FreeCAD python, but it is not complete. I'm not sure how to color the faces in python. To use the script create a new macro in the Macros menu and copy/paste this into it, then run the macro.
To colour a face you have to set the color in the Objects ViewProvider.

Also programming in a FreeCAD python script is NOT going to solve the problem if the shapes overlap, the only way is to avoid overlapping shapes.
Thanks.

It won't solve the problem, but it might help to narrow down where the problem is by examining the individual extrude objects. For example, Extrude001 clearly has an issue, indicating an error (perhaps mine, perhaps his) in the coordinates used.
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: How to use expressions as in OpenScad

Post by keithsloan52 »

TheMarkster wrote: Thu Jul 22, 2021 4:53 pm It won't solve the problem, but it might help to narrow down where the problem is by examining the individual extrude objects. For example, Extrude001 clearly has an issue, indicating an error (perhaps mine, perhaps his) in the coordinates used.
Not convinced. See my previous post about pasting the shorts bits of code from the CSG file. If you do them one at a time you see the Shapes created and it becomes very obvious where the overlaps are.

Another way is just to edit the CSG and comment out the blocks inturn, one block per shape, there only 13.

In fact its probably easier to debug in OpenSCAD see https://en.wikibooks.org/wiki/OpenSCAD_ ... Characters
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: How to use expressions as in OpenScad

Post by TheMarkster »

keithsloan52 wrote: Thu Jul 22, 2021 5:09 pm
TheMarkster wrote: Thu Jul 22, 2021 4:53 pm It won't solve the problem, but it might help to narrow down where the problem is by examining the individual extrude objects. For example, Extrude001 clearly has an issue, indicating an error (perhaps mine, perhaps his) in the coordinates used.
Not convinced. See my previous post about pasting the shorts bits of code from the CSG file. If you do them one at a time you see the Shapes created and it becomes very obvious where the overlaps are.

Another way is just to edit the CSG and comment out the blocks inturn, one block per shape, there only 13.

In fact its probably easier to debug in OpenSCAD see https://en.wikibooks.org/wiki/OpenSCAD_ ... Characters
You could be right. Still, if somebody is going to be using FreeCAD and doing scripting I say it's better to learn python rather than use openSCAD as an intermediary. But I'm no expert on openSCAD and really just an amateur hack with FreeCAD python.
User avatar
g0erwt
Posts: 11
Joined: Mon Jul 19, 2021 3:45 pm
Contact:

Re: How to use expressions as in OpenScad

Post by g0erwt »

TheMarkster wrote: Thu Jul 22, 2021 5:45 pm (cut....) Still, if somebody is going to be using FreeCAD and doing scripting I say it's better to learn python rather than use openSCAD as an intermediary. But I'm no expert on openSCAD and really just an amateur hack with FreeCAD python.
Hello Markster,

In general terms there are several possibilities to do modelling. I've a mathematical background which is one of the reasons that I prefer to use a language that I'm familar with: Mathematics. It's logic, concise and in most cases strait forward. Learning new things is becoming more and more difficult at my age (:-)). The step from Algol 60, via Fortran IV, to Tcl/TK and RUST is what I've managed. The step to Python will also be possible with some effort. I'm not so sure that scripting in combination with Python is a better way than using a tool that is more convenient to me.
Maybe I've to be more carefull to choose which expressions to use and in combination with expressions, like {color} to set properties.

As I said I'm new to FreeCAD and I don't like GUI's. The alternatives are not so obvious.
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: How to use expressions as in OpenScad

Post by keithsloan52 »

g0erwt wrote: Thu Jul 22, 2021 6:06 pm In general terms there are several possibilities to do modelling. I've a mathematical background which is one of the reasons that I prefer to use a language that I'm familar with: Mathematics. It's logic, concise and in most cases strait forward.
Well I would say you should revisit your Mathematics and not draw your model with overlapping shapes.
User avatar
g0erwt
Posts: 11
Joined: Mon Jul 19, 2021 3:45 pm
Contact:

Re: How to use expressions as in OpenScad

Post by g0erwt »

Hello everybody,

here are new results following the (early) suggestions by KeithSloan52.
First of all I did an export of the original OpenSCAD {filename.scad} to {fielname.csg}
In FreeCAD I imported that file and made a schreenshot of the result.

Note:
The OpenSCAD polygons, describing the form of the pieces of the puzzle are converted into [[wire], [wire001] ....[wire012]]
In the figure you now see that the expressions of the polygons with 4 vertices have been converted into 4 triangles.
I've no idea what has been done with the {color} settings.

I'll put some effort in it to work with the model step by step. The same way as I did when I developed the set of pieces. Maybe that will bring more evidence of what is happening in the FreeCAD OpenSCAD-Workbench.
FreeCAD-OpenSCAD-Workbench_new_result.png
FreeCAD-OpenSCAD-Workbench_new_result.png (217.44 KiB) Viewed 461 times
Post Reply