[Solved] Shape get back around origin

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

[Solved] Shape get back around origin

Post by Vincent B »

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 27 times
Last edited by Vincent B on Tue Nov 12, 2019 7:40 pm, edited 1 time in total.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Shape get back around origin

Post by openBrain »

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
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Shape get back around origin

Post by Vincent B »

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
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Shape get back around origin

Post by openBrain »

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
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Shape get back around origin

Post by DeepSOIC »

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
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Shape get back around origin

Post by openBrain »

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
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Shape get back around origin

Post by DeepSOIC »

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
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Shape get back around origin

Post by DeepSOIC »

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
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Shape get back around origin

Post by openBrain »

Thanks @DeepSOIC
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Shape get back around origin

Post by Vincent B »

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