Files
create/src/Mod/Draft/draftfunctions/join.py
Roy-043 d587e6a91a 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
2025-01-20 18:23:36 +01:00

90 lines
4.1 KiB
Python

# ***************************************************************************
# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * Copyright (c) 2020 FreeCAD Developers *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Provides functions to join wires together into a single wire."""
## @package join
# \ingroup draftfunctions
# \brief Provides functions to join wires together into a single wire.
## \addtogroup draftfunctions
# @{
import FreeCAD as App
import DraftVecUtils
def join_wires(wires, joinAttempts = 0):
"""join_wires(objects): merges a set of wires where possible, if any of those
wires have a coincident start and end point"""
wires = list(wires)
if joinAttempts > len(wires):
return
joinAttempts += 1
for wire1Index, wire1 in enumerate(wires):
for wire2Index, wire2 in enumerate(wires):
if wire2Index <= wire1Index:
continue
if join_two_wires(wire1, wire2):
wires.pop(wire2Index)
break
join_wires(wires, joinAttempts)
return wires
joinWires = join_wires
def join_two_wires(wire1, wire2):
"""join_two_wires(object, object): joins two wires if they share a common
point as a start or an end.
"""
wire1AbsPoints = [wire1.Placement.multVec(point) for point in wire1.Points]
wire2AbsPoints = [wire2.Placement.multVec(point) for point in wire2.Points]
if ((DraftVecUtils.equals(wire1AbsPoints[0], wire2AbsPoints[-1])
and DraftVecUtils.equals(wire1AbsPoints[-1], wire2AbsPoints[0]))
or (DraftVecUtils.equals(wire1AbsPoints[0], wire2AbsPoints[0])
and DraftVecUtils.equals(wire1AbsPoints[-1], wire2AbsPoints[-1]))):
wire2AbsPoints.pop()
wire1.Closed = True
elif DraftVecUtils.equals(wire1AbsPoints[0], wire2AbsPoints[0]):
wire1AbsPoints = list(reversed(wire1AbsPoints))
elif DraftVecUtils.equals(wire1AbsPoints[0], wire2AbsPoints[-1]):
wire1AbsPoints = list(reversed(wire1AbsPoints))
wire2AbsPoints = list(reversed(wire2AbsPoints))
elif DraftVecUtils.equals(wire1AbsPoints[-1], wire2AbsPoints[-1]):
wire2AbsPoints = list(reversed(wire2AbsPoints))
elif DraftVecUtils.equals(wire1AbsPoints[-1], wire2AbsPoints[0]):
pass
else:
return False
wire2AbsPoints.pop(0)
wire1.Points = ([wire1.Placement.inverse().multVec(point)
for point in wire1AbsPoints] +
[wire1.Placement.inverse().multVec(point)
for point in wire2AbsPoints])
App.ActiveDocument.removeObject(wire2.Name)
return True
joinTwoWires = join_two_wires
## @}