Draft: Make gui_tool_utils.get_support nesting aware (#10373)
The get_support function did not work for objects nested in, for example, Std_Parts. Additionally improved and cleaned up the code, and reduced the docstrings.
This commit is contained in:
@@ -220,31 +220,19 @@ getPoint = get_point
|
||||
|
||||
|
||||
def set_working_plane_to_object_under_cursor(mouseEvent):
|
||||
"""Set the working plane to the object under the cursor.
|
||||
"""Align the working plane to the face under the cursor.
|
||||
|
||||
It tests for an object under the cursor.
|
||||
If it is found, it checks whether a `'face'` or `'curve'` component
|
||||
is selected in the object's `Shape`.
|
||||
Then it tries to align the working plane to that face or curve.
|
||||
|
||||
The working plane is only aligned to the face if
|
||||
the working plane is not `'weak'`.
|
||||
The working plane is only aligned if it is `'weak'`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mouseEvent: Coin event
|
||||
Coin event with the mouse, that is, a click.
|
||||
Coin mouse event.
|
||||
|
||||
Returns
|
||||
-------
|
||||
None
|
||||
If no object was found in the 3D view under the cursor.
|
||||
Or if the working plane is not `weak`.
|
||||
Or if there was an exception with aligning the working plane
|
||||
to the component under the cursor.
|
||||
|
||||
Coin info
|
||||
The `getObjectInfo` of the object under the cursor.
|
||||
App::DocumentObject or None
|
||||
The parent object the face belongs to, if alignment occurred, or None.
|
||||
"""
|
||||
objectUnderCursor = gui_utils.get_3d_view().getObjectInfo((
|
||||
mouseEvent["Position"][0],
|
||||
@@ -252,25 +240,27 @@ def set_working_plane_to_object_under_cursor(mouseEvent):
|
||||
|
||||
if not objectUnderCursor:
|
||||
return None
|
||||
if "Face" not in objectUnderCursor["Component"]:
|
||||
return None
|
||||
wp = App.DraftWorkingPlane
|
||||
if wp.weak is False:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Get the component "face" or "curve" under the "Shape"
|
||||
# of the selected object
|
||||
componentUnderCursor = getattr(
|
||||
App.ActiveDocument.getObject(objectUnderCursor['Object']).Shape,
|
||||
objectUnderCursor["Component"])
|
||||
import Part
|
||||
if "ParentObject" in objectUnderCursor:
|
||||
obj = objectUnderCursor["ParentObject"]
|
||||
sub = objectUnderCursor["SubName"]
|
||||
else:
|
||||
obj = App.ActiveDocument.getObject(objectUnderCursor["Object"])
|
||||
sub = objectUnderCursor["Component"]
|
||||
shape = Part.getShape(obj, sub, needSubElement=True, retType=0)
|
||||
|
||||
if not App.DraftWorkingPlane.weak:
|
||||
return None
|
||||
|
||||
if "Face" in objectUnderCursor["Component"]:
|
||||
App.DraftWorkingPlane.alignToFace(componentUnderCursor)
|
||||
else:
|
||||
App.DraftWorkingPlane.alignToCurve(componentUnderCursor)
|
||||
App.DraftWorkingPlane.weak = True
|
||||
return objectUnderCursor
|
||||
except Exception:
|
||||
pass
|
||||
if wp.alignToFace(shape) is True:
|
||||
wp.weak = True
|
||||
if hasattr(Gui, "Snapper"):
|
||||
Gui.Snapper.setGrid()
|
||||
Gui.Snapper.restack()
|
||||
return obj
|
||||
|
||||
return None
|
||||
|
||||
@@ -279,32 +269,37 @@ setWorkingPlaneToObjectUnderCursor = set_working_plane_to_object_under_cursor
|
||||
|
||||
|
||||
def set_working_plane_to_selected_object():
|
||||
"""Set the working plane to the selected object's face.
|
||||
"""Align the working plane to a preselected face.
|
||||
|
||||
The working plane is only aligned to the face if
|
||||
the working plane is `'weak'`.
|
||||
The working plane is only aligned if it is `'weak'`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
None
|
||||
If more than one object was selected.
|
||||
Or if the selected object has many subelements.
|
||||
Or if the single subelement is not a `'Face'`.
|
||||
|
||||
App::DocumentObject
|
||||
The single object that contains a single selected face.
|
||||
App::DocumentObject or None
|
||||
The parent object the face belongs to, if alignment occurred, or None.
|
||||
"""
|
||||
sel = Gui.Selection.getSelectionEx()
|
||||
if len(sel) != 1:
|
||||
wp = App.DraftWorkingPlane
|
||||
if wp.weak is False:
|
||||
return None
|
||||
sel = sel[0]
|
||||
if (sel.HasSubObjects
|
||||
and len(sel.SubElementNames) == 1
|
||||
and "Face" in sel.SubElementNames[0]):
|
||||
if App.DraftWorkingPlane.weak:
|
||||
App.DraftWorkingPlane.alignToFace(sel.SubObjects[0])
|
||||
App.DraftWorkingPlane.weak = True
|
||||
return sel.Object
|
||||
|
||||
sels = Gui.Selection.getSelectionEx("", 0)
|
||||
|
||||
if len(sels) == 1 \
|
||||
and len(sels[0].SubObjects) == 1 \
|
||||
and sels[0].SubObjects[0].ShapeType == "Face":
|
||||
import Part
|
||||
shape = Part.getShape(sels[0].Object,
|
||||
sels[0].SubElementNames[0],
|
||||
needSubElement=True,
|
||||
retType=0)
|
||||
|
||||
if wp.alignToFace(shape) is True:
|
||||
wp.weak = True
|
||||
if hasattr(Gui, "Snapper"):
|
||||
Gui.Snapper.setGrid()
|
||||
Gui.Snapper.restack()
|
||||
return sels[0].Object
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@@ -312,43 +307,24 @@ setWorkingPlaneToSelectedObject = set_working_plane_to_selected_object
|
||||
|
||||
|
||||
def get_support(mouseEvent=None):
|
||||
"""Return the supporting object and set the working plane.
|
||||
""""Align the working plane to a preselected face or the face under the cursor.
|
||||
|
||||
It saves the current working plane, then sets it to the selected object.
|
||||
The working plane is only aligned if it is `'weak'`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mouseEvent: Coin event, optional
|
||||
It defaults to `None`.
|
||||
Coin event with the mouse, that is, a click.
|
||||
|
||||
If there is a mouse event it calls
|
||||
`set_working_plane_to_object_under_cursor`.
|
||||
Otherwise, it calls `set_working_plane_to_selected_object`.
|
||||
Defaults to `None`.
|
||||
Coin mouse event.
|
||||
|
||||
Returns
|
||||
-------
|
||||
None
|
||||
If there was a mouse event, but there was nothing under the cursor.
|
||||
Or if the working plane is not `weak`.
|
||||
Or if there was an exception with aligning the working plane
|
||||
to the component under the cursor.
|
||||
Or if more than one object was selected.
|
||||
Or if the selected object has many subelements.
|
||||
Or if the single subelement is not a `'Face'`.
|
||||
|
||||
Coin info
|
||||
If there was a mouse event, the `getObjectInfo`
|
||||
of the object under the cursor.
|
||||
|
||||
App::DocumentObject
|
||||
If there was no mouse event, the single selected object
|
||||
that contains the single selected face that was used
|
||||
to align the working plane.
|
||||
App::DocumentObject or None
|
||||
The parent object the face belongs to, if alignment occurred, or None.
|
||||
"""
|
||||
if mouseEvent:
|
||||
return set_working_plane_to_object_under_cursor(mouseEvent)
|
||||
return set_working_plane_to_selected_object()
|
||||
if mouseEvent is None:
|
||||
return set_working_plane_to_selected_object()
|
||||
return set_working_plane_to_object_under_cursor(mouseEvent)
|
||||
|
||||
|
||||
getSupport = get_support
|
||||
|
||||
Reference in New Issue
Block a user