Add FEM Constraint References Using Script

Need help, or want to share a macro? Post here!
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Add FEM Constraint References Using Script

Postby misterwazlib » Sun Jan 21, 2018 7:35 pm

Hi there,

I'm currently having problem with my python scripting. Like described in the title, I'm trying to add FEM constraint references using python script.
Usually we achieve this by using this code:

Code: Select all

heatflux_constraint.References = [(obj, "Face1"), (obj, "Face2"), (obj, "Face3"), (obj, "Face4"), (obj, "Face5"), (obj, "Face6")]
for a simple object like a cube with 6 faces it's easy to write. But for complex objects with hundreds or thousands of faces, this would be a problem for me. With these code I was able to select all faces of my object, but adding them as constraint references is another problem

Code: Select all

heatflux_constraint = ObjectsFem.makeConstraintHeatflux(doc, "FemConstraintHeatflux")
shapes = doc.findObjects("Part::Feature")
for sh in shapes:
	for i in range(len(sh.Shape.Faces)):
		face = "Face%d" % (i+1)
		heatflux_constraint.References = [(obj, face)]
		
which gives the outcome as follow : I have only the last face ("Face6") as my reference. Apparently the reference overwrites itself as the loop goes.

Does anybody have any idea how to fix this problem? I hope I described my problem clearly.

Thanks!
Dio
User avatar
bernd
Posts: 7717
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Add FEM Constraint References Using Script

Postby bernd » Sun Jan 21, 2018 8:30 pm

would you post the shape you used?
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Sun Jan 21, 2018 9:46 pm

I'm not sure what you meant by posting the shape so I uploaded my python script and the freecad file :)
Attachments
cube.py
(2.03 KiB) Downloaded 24 times
cube.FCStd
(6.65 KiB) Downloaded 10 times
User avatar
bernd
Posts: 7717
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Add FEM Constraint References Using Script

Postby bernd » Wed Jan 24, 2018 6:10 am

load your file and run:

Code: Select all

doc = App.ActiveDocument
boxobj = doc.cube
heatflux_constraint = doc.FemConstraintHeatflux
heatflux_constraint.References = []
print(heatflux_constraint.References)
for i in range(len(boxobj.Shape.Faces)):
    ref = (boxobj, "Face%d" % (i+1))
    tmp = heatflux_constraint.References
    tmp.append(ref)
    heatflux_constraint.References = tmp

doc.recompute()
print(heatflux_constraint.References)
for me is:

Code: Select all

>>>
>>> doc = App.ActiveDocument
>>> boxobj = doc.cube
>>> heatflux_constraint = doc.FemConstraintHeatflux
>>> heatflux_constraint.References = []
>>> print(heatflux_constraint.References)
[]
>>> for i in range(len(boxobj.Shape.Faces)):
...     ref = (boxobj, "Face%d" % (i+1))
...     tmp = heatflux_constraint.References
...     tmp.append(ref)
...     heatflux_constraint.References = tmp
... 
>>> doc.recompute()
2
>>> print(heatflux_constraint.References)
[(<Part::PartFeature>, ('Face1', 'Face2', 'Face3', 'Face4', 'Face5', 'Face6'))]
>>> 
:)

two problems in your code:
- you use an assignment but you would like to add new items to the references list. This is done by append
- the list of FreeCAD list property does not support append, you need to use a tmp list to append the reference and assign it to the property

hope it helps, Bernd
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Wed Jan 24, 2018 5:48 pm

bernd wrote:
Wed Jan 24, 2018 6:10 am

two problems in your code:
- you use an assignment but you would like to add new items to the references list. This is done by append
- the list of FreeCAD list property does not support append, you need to use a tmp list to append the reference and assign it to the property
so that's how you do it, thanks very much! :D
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Wed Jan 24, 2018 6:26 pm

bernd wrote:
Wed Jan 24, 2018 6:10 am

hope it helps, Bernd
Hi Bernd sorry for disturbing you again but I have another problem which keeps me up at night. The code works perfectly for usual CAD file (step, etc) but I was working on this .stl and .ply files which contain meshes. I followed the procedure which is to create the shape from mesh and then converting it into solid.
But the surfaces I got are distorted into little pieces like a mesh, I don't know if this is always the case with imported stl files but this makes it hard to add constraints. I thought about using the code to automatically select the many faces at one go but I got this result:

Code: Select all


>>> heatflux_constraint = ObjectsFem.makeConstraintHeatflux(doc, "FemConstraintHeatflux")
... >>> heatflux_constraint.References = []
... >>> for i in range(len(fus.Shape.Faces)):
... ... 	ref = (obj, "Face%d" % (i+1))
... ... 	tmp = heatflux_constraint.References
... ... 	tmp.append(ref)
... ... 	heatflux_constraint.References = tmp
... ... 
... Traceback (most recent call last):
...   File "<input>", line 1, in <module>
... ReferenceError: Cannot access attribute 'Shape' of deleted object

I also attached the .ply file if you want to take a look at it
Attachments
turbine.ply
(991.27 KiB) Downloaded 30 times
solid result after converting from mesh.PNG
solid result after converting from mesh.PNG (62.7 KiB) Viewed 1119 times
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Wed Jan 24, 2018 6:32 pm

you know what I realised my mistake there with the code so nevermind about that :oops: I forgot to change the object name

but my question still stands about the result of converting mesh into solid :mrgreen:
User avatar
bernd
Posts: 7717
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Add FEM Constraint References Using Script

Postby bernd » Wed Jan 24, 2018 7:05 pm

What do you expect if you convert mesh to solid? Sure you get all the faces from the mesh in your solid. If you would like to get curved faces you need to use some reverse engineering tool to find these curves, butmostly it is simpler to remodel the geometry. Or ask the one who exported if he could give you a step or brep. These file formats do support curved faces.

Ahh for so many fsces the following code is much much faster: https://forum.freecadweb.org/viewtopic. ... 20#p211388
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Wed Jan 24, 2018 9:00 pm

ah okay I was hoping there was another trick to convert it to curved faces. thank you! :)
misterwazlib
Posts: 39
Joined: Mon Dec 04, 2017 10:05 pm

Re: Add FEM Constraint References Using Script

Postby misterwazlib » Thu Feb 15, 2018 8:31 pm

bernd wrote:
Wed Jan 24, 2018 7:05 pm
Ahh for so many fsces the following code is much much faster: https://forum.freecadweb.org/viewtopic. ... 20#p211388
This code works normally, but in this case curiously it does not.

Code: Select all

>>> faces = []
>>> for i, face in enumerate(obj.Shape.Faces):
... 	faces.append("Face%d" % (i+1))
... 	tmpheatflux = (obj, tuple(faces))
... 
>>> doc.recompute()
4
>>> heatflux_constraint = ObjectsFem.makeConstraintHeatflux(doc, "FemConstraintHeatflux")
>>> heatflux_constraint.References = tmpheatflux
Traceback (most recent call last):
  File "<input>", line 1, in <module>
Base.FreeCADError: Unknown C++ exception
do you know why that is?
Attachments
testfile.FCStd
(239 KiB) Downloaded 11 times