[Macro] Snap Dimension lines to same length / re-center dimension

Discussions about the development of the TechDraw workbench
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

[Macro] Snap Dimension lines to same length / re-center dimension

Post by reox »

I have two idea what could improve elegancy of dimension lines:
1) if you have several dimensions next to each other (chain meausurement), it would be nice to be able to have all dimensions at the same length
2) You can move around the dimension number itself, but this might be by accident. Would it be possible to reset it to the center of the dimension line?

So for example:
Before the adjustments
Before the adjustments
2019-09-01-195832_242x629_scrot.png (9.58 KiB) Viewed 3460 times
After the adjustments
After the adjustments
2019-09-01-195552_272x612_scrot.png (9.47 KiB) Viewed 3460 times
Edit: Here is the macro to actually do that: https://github.com/reox/FreeCAD_macros/ ... re.FCMacro
Last edited by reox on Mon Sep 02, 2019 7:58 am, edited 1 time in total.
User avatar
wandererfan
Veteran
Posts: 6307
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by wandererfan »

reox wrote: Sun Sep 01, 2019 6:01 pm I have two idea what could improve elegancy of dimension lines:
1) if you have several dimensions next to each other (chain meausurement), it would be nice to be able to have all dimensions at the same length
2) You can move around the dimension number itself, but this might be by accident. Would it be possible to reset it to the center of the dimension line?
How about a chain dimension macro? The X and Y properties of the Dimension control where the text is located on the page. Collect some dimensions, average the Xs and Ys then update. The dimension and extension lines follow the text.

Probably quicker than waiting for me to code it.
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by reox »

That is a good idea. Should not be too hard!

A question for the macros: how can I check the instance of an object? For example I would like to know if all selected items are of type DrawViewDimension, but where is this type stored?

Code: Select all

>>> isinstance(App.ActiveDocument.Dimension009, TechDraw.DrawViewDimension)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'TechDraw' is not defined
>>> import TechDraw
>>> isinstance(App.ActiveDocument.Dimension009, TechDraw.DrawViewDimension)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: module 'TechDraw' has no attribute 'DrawViewDimension'
>>> dir(TechDraw)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'edgeWalker', 'findCentroid', 'findOuterWire', 'findShapeOutline', 'viewPartAsDxf', 'viewPartAsSvg', 'writeDXFPage', 'writeDXFView']
right now, I'm using this workaround:

Code: Select all

>>> App.ActiveDocument.Dimension009.__class__.__name__ == 'DrawViewDimension'
True
In the code from TechDraw I found this:

Code: Select all

>>> App.ActiveDocument.Dimension009.isDerivedFrom("TechDraw::DrawViewDimension")
True
Is that the correct way?
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by reox »

So, this would be the chain measure macro:
https://github.com/reox/FreeCAD_macros/ ... re.FCMacro

works really well!

A little bit more complicated is the re-centering of the value. I suppose the two points can be gathered using .getLinearPoints(), but I do not understand the transformation from these coordinates to the X/Y coordinate of the dimension? If you could hint me with the according lines of code in FC I can probably find out :)

Okay, found it. There are actually two different coordinate systems using the same origin, but they using different handyness, right?
So you simply take the average of the two coordinates but multiply the y coordinate by -1.
I added that now in the macro too!
User avatar
wandererfan
Veteran
Posts: 6307
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by wandererfan »

reox wrote: Mon Sep 02, 2019 6:33 am In the code from TechDraw I found this:

Code: Select all

>>> App.ActiveDocument.Dimension009.isDerivedFrom("TechDraw::DrawViewDimension")
True
Is that the correct way?
That is the standard way of checking if an object is a DrawViewDimension or one of its descendents.

If you want to check that it is a DVD but not a descendent, you can use this:

Code: Select all

>>> d = App.ActiveDocument.Dimension
>>> d.TypeId
'TechDraw::DrawViewDimension'
>>> 
In this case it doesn't matter, because DVD has no derived class at the moment.
User avatar
wandererfan
Veteran
Posts: 6307
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by wandererfan »

reox wrote: Mon Sep 02, 2019 7:09 am Okay, found it. There are actually two different coordinate systems using the same origin, but they using different handyness, right?
So you simply take the average of the two coordinates but multiply the y coordinate by -1.
You've discovered one of the biggest PITA's in TechDraw. Model space uses garden variety 3d coords. Paper space uses Qt's 2d coords that have (0,0) at the upper left, +X pointing to the right, but +Y pointing down the screen/page.

Sometimes this is handled internally, but I've lost hours debugging things that only required inversion of Y.
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Snap Dimension lines to same length / re-center dimension

Post by reox »

wandererfan wrote: Mon Sep 02, 2019 11:57 am
reox wrote: Mon Sep 02, 2019 7:09 am Okay, found it. There are actually two different coordinate systems using the same origin, but they using different handyness, right?
So you simply take the average of the two coordinates but multiply the y coordinate by -1.
You've discovered one of the biggest PITA's in TechDraw. Model space uses garden variety 3d coords. Paper space uses Qt's 2d coords that have (0,0) at the upper left, +X pointing to the right, but +Y pointing down the screen/page.
*g* I remember when I first started doing stuff with computer graphics and all of it sudden, they would use left handed coordinate systems all over the place. But in mechanical engineering, the only valid coordinate system is right handed :mrgreen:
I always wonder why the computer graphics people dont want to adopt to the good stuff :lol:

thanks for the information!
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: [Macro] Snap Dimension lines to same length / re-center dimension

Post by reox »

It isn't too hard to calculate the coordinates for the Distance type, if you do a little bit of vector calculations :lol:

So, the macro supports now DistanceX, DistanceY and Distance types!
2019-09-09-201502_729x562_scrot.png
2019-09-09-201502_729x562_scrot.png (31.58 KiB) Viewed 3297 times
User avatar
wandererfan
Veteran
Posts: 6307
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: [Macro] Snap Dimension lines to same length / re-center dimension

Post by wandererfan »

reox wrote: Mon Sep 09, 2019 6:17 pm So, the macro supports now DistanceX, DistanceY and Distance types!
Awesome!
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: [Macro] Snap Dimension lines to same length / re-center dimension

Post by galou_breizh »

reox wrote: Here is the macro to actually do that: https://github.com/reox/FreeCAD_macros/ ... re.FCMacro
Thanks a lot for this macro! Would it be actually possible that you publish it to the official FreeCAD macro repository, https://github.com/FreeCAD/FreeCAD-macros (according to the few criteria defined there)? Maybe other macros from your repository as well?

Thanks a lot,
Gaël
Post Reply