[Solved] Shape get back around origin

Need help, or want to share a macro? Post here!
User avatar
GlouGlou
Posts: 1716
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

[Solved] Shape get back around origin

Postby GlouGlou » Sat Nov 09, 2019 9:21 pm

I'm learning python, and I'm trying to build a parametric object like a egg.
After moved it on Z axle, and when I change a parameter It's remade around the origin.
How I can avoid that?
Attachments
Ovoide.py
(1.76 KiB) Downloaded 9 times
Last edited by GlouGlou on Tue Nov 12, 2019 7:40 pm, edited 1 time in total.
openBrain
Posts: 2332
Joined: Fri Nov 09, 2018 5:38 pm

Re: Shape get back around origin

Postby openBrain » Sat Nov 09, 2019 9:42 pm

Salut GlouGlou,

Very challenging for learning Python. ;)
I only have a very quick look (not easy to understand without any comment in the code), but I saw this line in execute() :

Code: Select all

matriz.append(FreeCAD.Vector(x,y,0))
In this line, Z is always set to 0.
Don't know if it's the reason, but the only clue I have for you ATM. ;)
User avatar
GlouGlou
Posts: 1716
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Shape get back around origin

Postby GlouGlou » Sat Nov 09, 2019 9:52 pm

z still =0 because it's a 2D curve.
If you move a cube or any shape (placement), and change his parameter, it don't come back on origins.
What I have missed?
openBrain
Posts: 2332
Joined: Fri Nov 09, 2018 5:38 pm

Re: Shape get back around origin

Postby openBrain » Tue Nov 12, 2019 4:10 pm

Hi GlouGlou,

Don't have a very precise explanation ATM, but it solves the problem if I delete the 'onChanged' function (which seems useless as the 'execute' function is launched automatically as soon as a property --placement, radius, ...-- is changed). ;)

The weird thing is that when 'execute' is launched from your macro with the line :

Code: Select all

self.execute(fp)
then the placement is reset by the line :

Code: Select all

fp.Shape = curve.toShape()
In other cases (when 'execute' is self launched by FC), the latter line doesn't reset the placement. I can't explain why ATM... :?
User avatar
DeepSOIC
Posts: 7145
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Shape get back around origin

Postby DeepSOIC » Tue Nov 12, 2019 4:22 pm

In execute(..), assign Placement attribute of the shape to fp.Placement before assigning the Shape property.

When FreeCAD recompute's your feature, it does that internally. However, from onChanged, it is not done implicitly, so you should do it explicitly.
openBrain
Posts: 2332
Joined: Fri Nov 09, 2018 5:38 pm

Re: Shape get back around origin

Postby openBrain » Tue Nov 12, 2019 4:28 pm

DeepSOIC wrote:
Tue Nov 12, 2019 4:22 pm
In execute(..), assign Placement attribute of the shape to fp.Placement before assigning the Shape property.

When FreeCAD recompute's your feature, it does that internally. However, from onChanged, it is not done implicitly, so you should do it explicitly.
Well corroborate what happens, but not clear for me how to assign the placement.
ATM I can only see the (obviously useless) :

Code: Select all

fp.Placement = fp.Placement
Edit : or do you mean something like :

Code: Select all

currPlacement = fp.Placement
fp.Shape = ... # change shape here
fp.Placement = currPlacement #re-assign placement
User avatar
DeepSOIC
Posts: 7145
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Shape get back around origin

Postby DeepSOIC » Tue Nov 12, 2019 4:36 pm

No.
This is how the end of your execute should look like:

Code: Select all

      ...
      if fp.createFace == True:
         sh = Part.Face(Part.Wire(curve.toShape()))
      else:
         sh = curve.toShape()
      sh.Placement = fp.Placement
      fp.Shape = sh
User avatar
DeepSOIC
Posts: 7145
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Shape get back around origin

Postby DeepSOIC » Tue Nov 12, 2019 4:38 pm

BTW, I wanted to find that piece of code in C++, but I can't. Probably, realthunder changed it. So I'm not sure about how it all works now.
openBrain
Posts: 2332
Joined: Fri Nov 09, 2018 5:38 pm

Re: Shape get back around origin

Postby openBrain » Tue Nov 12, 2019 6:06 pm

Thanks @DeepSOIC
User avatar
GlouGlou
Posts: 1716
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Shape get back around origin

Postby GlouGlou » Tue Nov 12, 2019 7:39 pm

Image
Thanks a lot.
next step: insert a icon inside the tree view.