Comparing placements using "isEqual" sets Touched, Bug??

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
freedman
Veteran
Posts: 3436
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Comparing placements using "isEqual" sets Touched, Bug??

Post by freedman »

First, I presume the phrase Dynamic data is the same as added property.

In a macro I do many compares using "isEqual" and "isSame" to dynamic data. As a test, if I compare a Placement to itself using local data then all is fine but if I compare a placement to Dynamic data then Touched is set. For this post I don't care about the compare out-come, I have issues with Touched. This seems broken, doing a compare should not change the state of the model. Here is a test macro.

Code: Select all

tolerance = .0001
print(" First test --------------", App.activeDocument().isTouched())     

for obj in App.activeDocument().Objects:
    if obj.TypeId == "Sketcher::SketchObject":  
        if not hasattr(obj,"Copy_placement"):  # add property 
            obj.addProperty("App::PropertyPlacement","Copy_placement")
            obj.Copy_placement = obj.Placement     # copy the placement      
        print("Found sketch")
        print( App.activeDocument().isTouched())  
#        if obj.Placement.Base.isEqual(obj.Copy_placement.Base, tolerance):
#            None
        print( App.activeDocument().isTouched())
You need to create a minimal model with a sketch. If you run the macro as is, Touched will stay False but if you uncomment the isEqual script and run it Touched will be set True. I don't know why this occurs.
Thanks
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by onekk »

freedman wrote: Sun Oct 02, 2022 4:01 pm ...
It is not the commented lines that cause the problem.

Problem is different, try this code:

Code: Select all

tolerance = .0001
print(" First test --------------", App.activeDocument().isTouched())     

for obj in App.activeDocument().Objects:
   if obj.TypeId == "Sketcher::SketchObject":  
        if not hasattr(obj,"Copy_placement"):  # add property 
            obj.addProperty("App::PropertyPlacement","Copy_placement")
            obj.Copy_placement = obj.Placement     # copy the placement
            obj.recompute()     
        print("Found sketch")
        print( App.activeDocument().isTouched())  
#        if obj.Placement.Base.isEqual(obj.Copy_placement.Base, tolerance):
#            None
        print( App.activeDocument().isTouched())
you have modified the object with:

Code: Select all

   if obj.TypeId == "Sketcher::SketchObject":  
        if not hasattr(obj,"Copy_placement"):  # add property 
            obj.addProperty("App::PropertyPlacement","Copy_placement")
            obj.Copy_placement = obj.Placement     # copy the placement
But the recompute has not been properly triggered, so you are comparing an "old state" so the False printed.


In my code I have correctly issued the object recomputing when you add Copy_placement property "et voila" the test is showing True, with the "offending" code left commented out.


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/
freedman
Veteran
Posts: 3436
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by freedman »

onekk,
This runs the first pass only to init the new property. Run the macro multiple times afterwards to see the error.

Code: Select all

obj.addProperty("App::PropertyPlacement","Copy_placement")
obj.Copy_placement = obj.Placement     # copy the placement
obj.recompute()  
For this post I don't care about the compare out-come, I have issues with Touched.

This situation is not good for what I'm trying to do, it means that every time I compare a placement (to dynamic data) I will need to do a recompute to clear Touched.

I see this in 0.20 and 0.21. Maybe this should be in the "Help section".
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by onekk »

EDIT: I'm trying to shorten things, to not crowd the post with not useful things.
Last edited by onekk on Wed Oct 05, 2022 10:31 am, edited 2 times in total.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by Roy_043 »

Seems like a bug to me. One way to workaround this (in this case):

Code: Select all

App.Vector(obj.Placement.Base).isEqual(obj.Copy_placement.Base, tolerance)
freedman
Veteran
Posts: 3436
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by freedman »

Roy_043 wrote
Seems like a bug to me. One way to workaround this (in this case):
Tested!

Code: Select all

if App.Vector(obj.Placement.Base).isEqual(obj.Copy_placement.Base, tolerance):
That works! This runs and Touched stays False as it should in this case.

Edit: For the rotation use: App.Rotation

Reading data certainly shouldn't affect the model status so I will write this up as a bug. I won't question the difference in coding but I will do testing, I hope this new script will run faster.

Thanks Roy_043
Last edited by freedman on Wed Oct 05, 2022 5:48 am, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by onekk »

EDIT: This post was not adding significative things as solution is found and has proven to be a bug.
Last edited by onekk on Wed Oct 05, 2022 10:29 am, edited 3 times in total.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by Roy_043 »

Code: Select all

FreeCAD.Placement.Base
Is not the same as:

Code: Select all

FreeCAD.Placement().Base
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by openBrain »

https://github.com/FreeCAD/FreeCAD/pull/7559

@freedman Thanks for reporting.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Comparing placements using "isEqual" sets Touched, Bug??

Post by openBrain »

Merged
Post Reply