Draft: make move, rotate and scale commands link-aware (#18795)

* Draft: make move, rotate and scale commands link-aware

Fixes #12836.
Fixes #15681.

These changes are presented as a single ('big') PR because the mentioned commands have several similarities. Working on them seperately would have made less sense.

The commands have been made 'link-aware' meaning they can now handle Links and objects in linked containers.

This required several changes in the following main files. For each command all options are handled by a single function now (the `move`, `rotate` and `scale` functions). This was the only reasonable solution to correctly handle nested placements. As a result there is no longer a need to build very complex 'cmd' strings in the gui_*.py files (which is a good thing IMO).

Main files:

* move.py
* rotate.py
* scale.py
* gui_move.py
* gui_rotate.py
* gui_scale.py
* gui_trackers.py

The following files have also been updated:

* Draft.py: Imports updated.
* DraftGui.py: If `CopyMode` is changed the ghosts have to be updated. The move and rotate commands now also show previews of movable children. But since those are not copied they should be removed from the ghosts if `CopyMode` is changed to `True`.
* utils.py: Some helper functions have been added. An existing helper function (only used internally) has been renamed.
* gui_utils.py: The `select` function has been updated to accept a list of tuples to allow the reselection of nested objects.
* clone.py: A new property `ForceCompound`, necessary for non-uniform scaling, has been added.
* join.py: The `join_wires` function now returns the resultant wire objects.
* task_scale.py: Updated to allow negative scale factors. Support for `SubelementMode` preference added.
* dimension.py: `transform` methods added.
* layer.py: `get_layer` function added.
* svg.py: Updated to use `get_layer`.
* view_text.py: Instead of two added `coin.SoTransform()` nodes the main transform node is used instead. This was done so that ghosts of Draft Texts can be handled properly without requiring dedicated code in gui_trackers.py.

Notes:

* Support for "App::Annotation" is limited. Only their `Position` is handled (by the move and rotate commands).
* Support for "Image::ImagePlane" has been removed from the scale command. The object has its own calibrate feature (see https://wiki.freecad.org/Std_Import).
* Copies and clones are always created in the global space.

* Fix several unit test issues.

* Reset value that was changed while testing

* Rebase and update test_modification.py

* Reintroduce scaling of image planes
This commit is contained in:
Roy-043
2025-01-20 18:23:36 +01:00
committed by GitHub
parent a123ebd6d6
commit 2ee620ab42
19 changed files with 1042 additions and 973 deletions

View File

@@ -33,31 +33,35 @@ import FreeCAD as App
import DraftVecUtils
from draftobjects.base import DraftObject
from draftutils import gui_utils
from draftutils.messages import _wrn
from draftutils.translate import translate
class Clone(DraftObject):
"""The Clone object"""
def __init__(self,obj):
def __init__(self, obj):
self.set_properties(obj)
super().__init__(obj, "Clone")
_tip = QT_TRANSLATE_NOOP("App::Property",
"The objects included in this clone")
obj.addProperty("App::PropertyLinkListGlobal", "Objects",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"The scale factor of this clone")
obj.addProperty("App::PropertyVector", "Scale",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"If Clones includes several objects,\n"
"set True for fusion or False for compound")
obj.addProperty("App::PropertyBool", "Fuse",
"Draft", _tip)
obj.Scale = App.Vector(1,1,1)
def set_properties(self, obj):
pl = obj.PropertiesList
if not "Objects" in pl:
_tip = QT_TRANSLATE_NOOP("App::Property", "The objects included in this clone")
obj.addProperty("App::PropertyLinkListGlobal", "Objects", "Draft", _tip)
if not "Scale" in pl:
_tip = QT_TRANSLATE_NOOP("App::Property", "The scale factor of this clone")
obj.addProperty("App::PropertyVector", "Scale", "Draft", _tip)
obj.Scale = App.Vector(1, 1, 1)
if not "Fuse" in pl:
_tip = QT_TRANSLATE_NOOP("App::Property",
"If Clones includes several objects,\n"
"set True for fusion or False for compound")
obj.addProperty("App::PropertyBool", "Fuse", "Draft", _tip)
if not "ForceCompound" in pl:
_tip = QT_TRANSLATE_NOOP("App::Property", "Always create a compound")
obj.addProperty("App::PropertyBool", "ForceCompound", "Draft", _tip)
def onDocumentRestored(self, obj):
super().onDocumentRestored(obj)
@@ -65,9 +69,14 @@ class Clone(DraftObject):
gui_utils.restore_view_object(
obj, vp_module="view_clone", vp_class="ViewProviderClone", format_ref=ref
)
if hasattr(obj, "ForceCompound"):
return
self.set_properties(obj)
_wrn("v1.1, " + obj.Label + ", " + translate("draft", "added 'ForceCompound' property"))
def join(self,obj,shapes):
fuse = getattr(obj, 'Fuse', False)
def join(self, obj, shapes):
fuse = getattr(obj, "Fuse", False)
force_compound = getattr(obj, "ForceCompound", False)
if fuse:
tmps = []
for s in shapes:
@@ -79,9 +88,12 @@ class Clone(DraftObject):
for s in shapes:
tmps += s.Edges
shapes = tmps
if len(shapes) == 1:
return shapes[0]
import Part
if len(shapes) == 1:
if force_compound:
return Part.makeCompound([shapes[0]])
else:
return shapes[0]
if fuse:
try:
sh = shapes[0].multiFuse(shapes[1:])
@@ -89,7 +101,10 @@ class Clone(DraftObject):
except Exception:
pass
else:
return sh
if force_compound:
return Part.makeCompound([sh])
else:
return sh
return Part.makeCompound(shapes)
def execute(self,obj):

View File

@@ -320,6 +320,14 @@ class LinearDimension(DimensionBase):
if hasattr(obj, "Support"):
obj.setPropertyStatus('Support', 'Hidden')
def transform(self, obj, pla):
"""Transform the object by applying a placement."""
obj.Start = pla.multVec(obj.Start)
obj.End = pla.multVec(obj.End)
obj.Dimline = pla.multVec(obj.Dimline)
obj.Normal = pla.Rotation.multVec(obj.Normal)
obj.Direction = pla.Rotation.multVec(obj.Direction)
def execute(self, obj):
"""Execute when the object is created or recomputed.
@@ -571,6 +579,24 @@ class AngularDimension(DimensionBase):
return
super().update_properties_0v21(obj, vobj)
def transform(self, obj, pla):
"""Transform the object by applying a placement."""
import Part
new_normal = pla.Rotation.multVec(obj.Normal)
c_old = Part.makeCircle(1, App.Vector(), obj.Normal)
c_tra = c_old.transformShape((pla * c_old.Placement).Rotation.Matrix)
c_new = Part.makeCircle(1, App.Vector(), new_normal)
delta_angle = math.degrees(c_new.Curve.parameter(c_tra.firstVertex().Point))
first_angle = (obj.FirstAngle.Value + delta_angle) % 360
last_angle = (obj.LastAngle.Value + delta_angle) % 360
obj.Center = pla.multVec(obj.Center)
obj.Dimline = pla.multVec(obj.Dimline)
obj.Normal = new_normal
obj.FirstAngle = first_angle
obj.LastAngle = last_angle
def execute(self, obj):
"""Execute when the object is created or recomputed.

View File

@@ -32,6 +32,7 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
from draftutils import gui_utils
from draftutils import utils
from draftutils.messages import _wrn
from draftutils.translate import translate
@@ -160,4 +161,21 @@ class LayerContainer:
if state:
self.Type = state
# Similar function as in view_layer.py
def get_layer(obj):
"""Get the layer the object belongs to."""
finds = obj.Document.findObjects(Name="LayerContainer")
if not finds:
return None
# First look in the LayerContainer:
for layer in finds[0].Group:
if utils.get_type(layer) == "Layer" and obj in layer.Group:
return layer
# If not found, look through all App::FeaturePython objects (not just layers):
for find in obj.Document.findObjects(Type="App::FeaturePython"):
if utils.get_type(find) == "Layer" and obj in find.Group:
return find
return None
## @}