Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

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
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by chennes »

I can fix the visibility issue with the stairs by re-writing the fixVisibility() function, but I'm not sure if this has negative consequences outside of this case, since I don't really understand the heuristics that were being used in the old one. @keithsloan52, can you have a look at this and make sure it's doing what you intended that function to do? The new version is quite a lot simpler because it relies on FreeCAD's internal "rootObject" notion, and the idea that coming out of the OpenSCAD converter, anything under a root object should be hidden. Obviously a real solution will have to cope with objects that weren't imported from OpenSCAD, but this is the core idea of the fix:

Code: Select all

def fixVisibility():
    for root_object in FreeCAD.ActiveDocument.RootObjects:
        root_object.ViewObject.Visibility=True
        for obj in root_object.OutListRecursive:
            obj.ViewObject.Visibility=False
keithsloan52 wrote: Wed Dec 15, 2021 3:45 pm Ping
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by keithsloan52 »

Hi Chris
Happy New Year
chennes wrote: Tue Jan 11, 2022 4:42 am I can fix the visibility issue with the stairs by re-writing the fixVisibility() function, but I'm not sure if this has negative consequences outside of this case, since I don't really understand the heuristics that were being used in the old one. @keithsloan52, can you have a look at this and make sure it's doing what you intended that function to do? The new version is quite a lot simpler because it relies on FreeCAD's internal "rootObject" notion, and the idea that coming out of the OpenSCAD converter, anything under a root object should be hidden. Obviously a real solution will have to cope with objects that weren't imported from OpenSCAD, but this is the core idea of the fix:

Code: Select all

def fixVisibility():
    for root_object in FreeCAD.ActiveDocument.RootObjects:
        root_object.ViewObject.Visibility=True
        for obj in root_object.OutListRecursive:
            obj.ViewObject.Visibility=False
keithsloan52 wrote: Wed Dec 15, 2021 3:45 pm Ping
By original fixVisibility I assume you mean

Code: Select all

def fixVisibility():
    for obj in FreeCAD.ActiveDocument.Objects:
         if(( obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse") and shallHide(obj)):
            if "Group" in obj.FullName:
                alreadyhidden.append(obj)
                obj.ViewObject.Visibility=False
                for currentObject in obj.OutList:
                    if(currentObject not in alreadyhidden):
                        currentObject.ViewObject.Visibility=True
Although it is in importCSG which has my copyright its not something I recognise could have been added by Sebastian.

I would go with your update.

I also note that the original code is followed by if gui: surely the whole thing should be subject to an if gui:

Keith
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by chennes »

keithsloan52 wrote: Tue Jan 11, 2022 8:33 am I also note that the original code is followed by if gui: surely the whole thing should be subject to an if gui:
Thanks for checking, I didn't bother with a git blame, but it doesn't really matter, I'm pretty confident this is what the old function was trying to do. It's only called once, inside an if_gui clause, so we could define the function only in an if_gui as well, but Python being Python, it doesn't actually matter much. I don't know why it's at the top of the file... I usually bury these "cheater" functions at the bottom!
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by chennes »

OK, git commit 1f70e352a modifies that "fix", and I now get the correct output for that ladder. I spot checked a few other OpenSCAD files I had sitting around and didn't see any regressions, but please report them here if you run across any.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
bvwj
Posts: 13
Joined: Wed Jan 19, 2022 10:20 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by bvwj »

I can bite out the corner of this square with difference, or I can round the corners with offset. Alone either work.

When I try to difference the corner, then round with offset, it fails.

Code: Select all

$fn=64; 
linear_extrude(2)
  offset(r = 2)
    difference(){
      square([20, 20],center=true);
      translate([10,10]) 
        circle(d = 10);
    }

Code: Select all

File "/Applications/FreeCAD.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 432, in p_offset_action
    if subobj[0].Shape.Volume == 0 :
<class 'RuntimeError'>: shape is invalid
test.png
test.png (9.3 KiB) Viewed 4649 times
OS: macOS 10.16
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.2)
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)
Last edited by bvwj on Wed Jan 19, 2022 10:40 pm, edited 2 times in total.
bvwj
Posts: 13
Joined: Wed Jan 19, 2022 10:20 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by bvwj »

Is the difference/offset/extrude method something that should work in FreeCAD, or is it off limits?

I like this method to generate printed circuit board outlines with radiused corners. It is a practical application, not just an interesting structure.

If it will not work I need to go back to adding all the fillets and rounding corners one at a time so I can get these boards into FreeCAD to design the rest of the housing.
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by chennes »

I was able to reproduce your error in the latest build of FreeCAD, but I have not had time to debug it (and probably won't for a few weeks). I don't see any obvious reason why it doesn't work, but maybe @keithsloan52 has some idea.
keithsloan52 wrote: Tue Jan 11, 2022 8:33 am .
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by keithsloan52 »

chennes wrote: Thu Jan 27, 2022 10:53 pm I was able to reproduce your error in the latest build of FreeCAD, but I have not had time to debug it (and probably won't for a few weeks). I don't see any obvious reason why it doesn't work, but maybe @keithsloan52 has some idea.
keithsloan52 wrote: Tue Jan 11, 2022 8:33 am .
Adding to the code

Code: Select all

    print(f'p6 : {p[6]}')
    print(dir(p[6]))
Report View looks like

Code: Select all

09:45:46  ImportCSG Version 0.6a
09:45:46  Start Lex
09:45:46  End Lex
09:45:46  Load Parser
09:45:46  Parser Loaded
09:45:46  Start Parser
09:45:46  ('height', '2')
09:45:46  ('center', 'false')
09:45:46  ('convexity', '1')
09:45:46  2d Point
09:45:46  ('scale', [1.0, 1.0])
09:45:46  ('$fn', '64')
09:45:46  ('$fa', '12')
09:45:46  ('$fs', '2')
09:45:46  ('r', '2')
09:45:46  ('$fn', '64')
09:45:46  ('$fa', '12')
09:45:46  ('$fs', '2')
09:45:46  2d Point
09:45:46  ('size', [20.0, 20.0])
09:45:46  ('center', 'true')
09:45:46  Square
09:45:46  Vector
09:45:46  Vector
09:45:46  Vector
09:45:46  Vector
09:45:46  Matrix
09:45:46  ('$fn', '64')
09:45:46  ('$fa', '12')
09:45:46  ('$fs', '2')
09:45:46  ('r', '5')
09:45:46  Circle : {'$fn': '64', '$fa': '12', '$fs': '2', 'r': '5'}
09:45:46  Circle Shape : True
09:45:46  Push Circle
09:45:46  MultMatrix
09:45:46  Multmatrix
09:45:46  [['1', '0', '0', '10'], ['0', '1', '0', '10'], ['0', '0', '1', '0'], ['0', '0', '0', '1']]
09:45:46  Matrix ((1,0,0,10),(0,1,0,10),(0,0,1,0),(0,0,0,1))
09:45:46  Apply Multmatrix
09:45:46  special orthogonal
09:45:46  rotation rounded
09:45:46  [<Part::Part2DObject>]
09:45:46  difference
09:45:46  2
09:45:46  [<Part::PartFeature>, <Part::Part2DObject>]
09:45:46  Push Resulting Cut
09:45:46  End Cut
09:45:46  p6 : [<Part::PartFeature>]
09:45:46  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
09:45:46  Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/keithsloan/Library/Preferences/FreeCAD/Mod/OpenSCAD_Alt_Import/freecad/OpenSCAD_Alt_Import/importCSG.py", line 96, in open
    processcsg(tmpfile)
  File "/Users/keithsloan/Library/Preferences/FreeCAD/Mod/OpenSCAD_Alt_Import/freecad/OpenSCAD_Alt_Import/importCSG.py", line 157, in processcsg
    result = parser.parse(f.read())
  File "/Applications/FreeCAD_19.2.app/Contents/Resources/lib/python3.8/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Applications/FreeCAD_19.2.app/Contents/Resources/lib/python3.8/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
    p.callable(pslice)
  File "/Users/keithsloan/Library/Preferences/FreeCAD/Mod/OpenSCAD_Alt_Import/freecad/OpenSCAD_Alt_Import/importCSG.py", line 411, in p_offset_action
    if subobj[0].Shape.Volume == 0 :
<class 'RuntimeError'>: shape is invalid
subobj = p6

So the code assumes it is dealing with an Object with a Shape where as in this case it is being passed a <Part::PartFeature> and it does not have a Shape. It looks like what is passed is the result of the cut.

Might have more time to look this evening
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by keithsloan52 »

Adding debug code to the difference i.e. cut the Push of the result looks okay

Code: Select all

10:04:26  difference
10:04:26  2
10:04:26  [<Part::PartFeature>, <Part::Part2DObject>]
10:04:26  Push Resulting Cut
10:04:26  ['Base', 'Content', 'Document', 'ExpressionEngine', 'FullName', 'History', 'ID', 'InList', 'InListRecursive', 'Label', 'Label2', 'MemSize', 'Module', 'MustExecute', 'Name', 'NoTouch', 'OldLabel', 'OutList', 'OutListRecursive', 'Parents', 'Placement', 'PropertiesList', 'Refine', 'Removing', 'Shape', 'State', 'Tool', 'TypeId', 'ViewObject', 'Visibility', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'addExtension', 'addProperty', 'adjustRelativeLinks', 'dumpContent', 'dumpPropertyContent', 'enforceRecompute', 'evalExpression', 'getAllDerivedFrom', 'getDocumentationOfProperty', 'getEditorMode', 'getEnumerationsOfProperty', 'getGlobalPlacement', 'getGroupOfProperty', 'getLinkedObject', 'getParentGeoFeatureGroup', 'getParentGroup', 'getPaths', 'getPathsByOutList', 'getPropertyByName', 'getPropertyNameOfGeometry', 'getPropertyOfGeometry', 'getPropertyStatus', 'getPropertyTouchList', 'getStatusString', 'getSubObject', 'getSubObjectList', 'getSubObjects', 'getTypeIdOfProperty', 'getTypeOfProperty', 'hasChildElement', 'hasExtension', 'isDerivedFrom', 'isElementVisible', 'isValid', 'purgeTouched', 'recompute', 'removeProperty', 'resolve', 'resolveSubElement', 'restoreContent', 'restorePropertyContent', 'setEditorMode', 'setElementVisible', 'setExpression', 'setPropertyStatus', 'supportedProperties', 'touch']
10:04:26  End Cut
10:04:26  p6 : [<Part::PartFeature>]
So clearly something is not right as this is what the 2DOffset should see
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problem OpenSCAD scad & csg files - PLEASE Add them to this thread - Thanks

Post by keithsloan52 »

Error in Offset Action

Code: Select all

File "/Applications/FreeCAD.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 432, in p_offset_action
    if subobj[0].Shape.Volume == 0 :
<class 'RuntimeError'>: shape is invalid
The current code expects the object to be offset to have a valid Shape, where as in this case it is a Part::Cut with just a Base and Tool Objects

I have tried to change the difference/cut code to create a Shape from the Base and Tool but I then hit the problem that in this case the Tool is a circle created with

Code: Select all

mycircle = FreeCAD.ActiveDocument.addObject("Part::Part2DObjectPython",'circle')
        Draft._Circle(mycircle)
        mycircle.Radius = r
        mycircle.MakeFace = True
        #mycircle = Draft.makeCircle(r,face=True) # would call doc.recompute
        #mycircle = doc.addObject('Part::Circle',p[1]) #would not create a face
        #mycircle.Radius = r
        print('Circle Shape : ' +str(mycircle.Shape.isNull()))
And this prints out that the circle shape is a Null i.e. does not have a valid Shape
@chennes any ideas?
Last edited by keithsloan52 on Sat Jan 29, 2022 8:22 pm, edited 1 time in total.
Post Reply