Part FeaturePython Shape from a file.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Part FeaturePython Shape from a file.

Post by keithsloan52 »

I wish to create a Part FeaturePython Object where the Shape is read from a brep file.

I have a property that is the path of the file but if I try and set the Shape with self.Object.Shape.read(path) in the def __init___
or an equivalent in createGeometry I get an error

Code: Select all

 File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/GDMLObjects.py", line 5159, in __init__
    self.Object.Shape.read(path)
<class 'ReferenceError'>: This object is immutable, you can not set any attribute or call a non const method
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Part FeaturePython Shape from a file.

Post by onekk »

Did you try:

Code: Select all

load_obj = Part.Shape()
load_obj.read(path)
self.Object.Shape = load_obj
not tested!

Hope it helps.

Kind 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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Part FeaturePython Shape from a file.

Post by keithsloan52 »

onekk wrote: Sun Apr 02, 2023 12:32 pm Did you try:

Code: Select all

load_obj = Part.Shape()
load_obj.read(path)
self.Object.Shape = load_obj
not tested!

Hope it helps.

Kind Regards

Carlo D.
Better but get

Code: Select all

14:54:52  <Part> ViewProviderExt.cpp(1270): Cannot compute Inventor representation for the shape of Dipole#GDMLbrepPart_DC2: Bnd_Box is void
14:54:52  <Part> ViewProviderExt.cpp(1270): Cannot compute Inventor representation for the shape of Dipole#GDMLbrepPart_DC2: Bnd_Box is void
If I print out the bound box and try and use it to set Shape.BoundBox with the following

Code: Select all

        loadShape = Part.Shape()
        loadShape.read(path)
        print(f"brep BoundBox {loadShape.BoundBox}")
        self.Object.Shape = loadShape
        self.Object.Shape.BoundBox = loadShape.BoundBox
I get ( Note BoundBox is VERY large )

Code: Select all

15:00:37  Path exists True
15:00:37  brep BoundBox BoundBox (1.79769e+308, 1.79769e+308, 1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308)
15:00:37  <Part> ViewProviderExt.cpp(1270): Cannot compute Inventor representation for the shape of Dipole#GDMLbrepPart_DC2: Bnd_Box is void
15:00:37  Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 96, in open
    processGDML(doc, True, filename, True, False)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 3066, in processGDML
    parseVolume(doc, volDict, part, world, phylvl, 3)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 2044, in parseVolume
    expandVolume(doc, volDict, parent, name, phylvl, displayMode)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 2407, in expandVolume
    parsePhysVol(
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 1978, in parsePhysVol
    expandVolume(doc, volDict, part, volRef, phylvl, displayMode)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 2407, in expandVolume
    parsePhysVol(
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 1978, in parsePhysVol
    expandVolume(doc, volDict, part, volRef, phylvl, displayMode)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 2399, in expandVolume
    processVol(doc, vol, volDict, parent, phylvl, displayMode)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/importGDML.py", line 2359, in processVol
    GDMLbrepPart(part, path)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/GDML/./freecad/gdml/GDMLObjects.py", line 5162, in __init__
    self.Object.Shape.BoundBox = loadShape.BoundBox
<class 'AttributeError'>: Attribute 'BoundBox' of object 'ComplexGeoData' is read-only
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Part FeaturePython Shape from a file.

Post by onekk »

There is no a fromBREP() method in Part.Shape? Or something similar.

Sorry no experiment as I'm far from a computer.

Regards

Carlo D.
Last edited by onekk on Sun Apr 02, 2023 3:40 pm, edited 2 times in total.
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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Part FeaturePython Shape from a file.

Post by keithsloan52 »

Okay found the problem the brep file was not valid.

It was created by selecting a Part and then exporting to Brep, that obviously does not work. If I select an object in a Part and then export that to Brep it creates a valid brep.

I guess to make it more general I need to select all Objects in a Part, make a Compound and then export that. Time to put think hat on again.
Post Reply