From 267291567fcb127fb97a68aa7a2cf51457d2db96 Mon Sep 17 00:00:00 2001 From: carlopav Date: Tue, 25 May 2021 01:03:20 +0200 Subject: [PATCH] Draft: Edit, cleanup of new GuiTools.addPoint methods In new splitted addPoint methods, getObjectsInfo and the consequent checks have been removed and moved to main DraftEdit module in a new get_specific_object_info method. This method returns the info for the selected object at a given position and the 3d Vector of the point clicked on the object. --- src/Mod/Draft/draftguitools/gui_edit.py | 14 ++ .../draftguitools/gui_edit_draft_objects.py | 130 +++++++----------- 2 files changed, 62 insertions(+), 82 deletions(-) diff --git a/src/Mod/Draft/draftguitools/gui_edit.py b/src/Mod/Draft/draftguitools/gui_edit.py index 90e8f30220..d334fac5e8 100644 --- a/src/Mod/Draft/draftguitools/gui_edit.py +++ b/src/Mod/Draft/draftguitools/gui_edit.py @@ -843,6 +843,20 @@ class Edit(gui_base_original.Modifier): obj_gui_tools.restore_object_style(obj, self.objs_formats[obj.Name]) + def get_specific_object_info(self, obj, pos): + """Return info of a specific object at a given position. + """ + selobjs = Gui.ActiveDocument.ActiveView.getObjectsInfo((pos[0],pos[1])) + if not selobjs: + return + for info in selobjs: + if not info: + continue + if obj.Name == info["Object"] and "x" in info: + # prefer "real" 3D location over working-plane-driven one if possible + pt = App.Vector(info["x"], info["y"], info["z"]) + return info, pt + def get_selected_obj_at_position(self, pos): """Return object at given position. diff --git a/src/Mod/Draft/draftguitools/gui_edit_draft_objects.py b/src/Mod/Draft/draftguitools/gui_edit_draft_objects.py index 854ebf1da6..bce3f5aaf3 100644 --- a/src/Mod/Draft/draftguitools/gui_edit_draft_objects.py +++ b/src/Mod/Draft/draftguitools/gui_edit_draft_objects.py @@ -129,40 +129,34 @@ class DraftWireGuiTools(GuiTools): def add_point(self, edit_command, obj, pos): """Add point to obj. """ - # self.setSelectState(obj, True) - selobjs = Gui.ActiveDocument.ActiveView.getObjectsInfo((pos[0],pos[1])) - if not selobjs: + info, newPoint = edit_command.get_specific_object_info(obj,pos) + + if not info: return - for info in selobjs: - if not info: - return - if obj.Name != info["Object"]: - return - if not 'Edge' in info["Component"]: - return - newPoint = App.Vector(info["x"], info["y"], info["z"]) - edgeIndex = int(info["Component"][4:]) + if not 'Edge' in info["Component"]: + return + edgeIndex = int(info["Component"][4:]) - newPoints = [] - if hasattr(obj, "ChamferSize") and hasattr(obj, "FilletRadius"): - # TODO: If Draft_Wire fails to calculate one of the fillets or chamfers - # this algo fails to identify the correct edge - if obj.ChamferSize > 0 and obj.FilletRadius > 0: - edgeIndex = (edgeIndex + 3) / 4 - elif obj.ChamferSize > 0 or obj.FilletRadius > 0: - edgeIndex = (edgeIndex + 1) / 2 + newPoints = [] + if hasattr(obj, "ChamferSize") and hasattr(obj, "FilletRadius"): + # TODO: If Draft_Wire fails to calculate one of the fillets or chamfers + # this algo fails to identify the correct edge + if obj.ChamferSize > 0 and obj.FilletRadius > 0: + edgeIndex = (edgeIndex + 3) / 4 + elif obj.ChamferSize > 0 or obj.FilletRadius > 0: + edgeIndex = (edgeIndex + 1) / 2 - for index, point in enumerate(obj.Points): - if index == edgeIndex: - newPoints.append(edit_command.localize_vector(obj, newPoint)) - newPoints.append(point) - if obj.Closed and edgeIndex == len(obj.Points): - # last segment when object is closed + for index, point in enumerate(obj.Points): + if index == edgeIndex: newPoints.append(edit_command.localize_vector(obj, newPoint)) - obj.Points = newPoints + newPoints.append(point) + if obj.Closed and edgeIndex == len(obj.Points): + # last segment when object is closed + newPoints.append(edit_command.localize_vector(obj, newPoint)) + obj.Points = newPoints - obj.recompute() - return + obj.recompute() + return def delete_point(self, obj, node_idx): if len(obj.Points) <= 2: @@ -195,40 +189,29 @@ class DraftBSplineGuiTools(DraftWireGuiTools): def add_point(self, edit_command, obj, pos): """Add point to obj. """ - # self.setSelectState(obj, True) - selobjs = Gui.ActiveDocument.ActiveView.getObjectsInfo((pos[0],pos[1])) - if not selobjs: + info, pt = edit_command.get_specific_object_info(obj,pos) + if not info or (pt is None): return - for info in selobjs: - if not info: - return - if obj.Name != info["Object"]: - return - if "x" in info:# prefer "real" 3D location over working-plane-driven one if possible - point = App.Vector(info["x"], info["y"], info["z"]) - else: - continue - pts = obj.Points - if (obj.Closed == True): - curve = obj.Shape.Edges[0].Curve - else: - curve = obj.Shape.Curve - uNewPoint = curve.parameter(point) - uPoints = [] - for p in obj.Points: - uPoints.append(curve.parameter(p)) - for i in range(len(uPoints) - 1): - if ( uNewPoint > uPoints[i] ) and ( uNewPoint < uPoints[i+1] ): - pts.insert(i + 1, edit_command.localize_vector(obj, point)) - break - # DNC: fix: add points to last segment if curve is closed - if obj.Closed and (uNewPoint > uPoints[-1]): - pts.append(edit_command.localize_vector(obj, point)) - obj.Points = pts + pts = obj.Points + if (obj.Closed == True): + curve = obj.Shape.Edges[0].Curve + else: + curve = obj.Shape.Curve + uNewPoint = curve.parameter(pt) + uPoints = [] + for p in obj.Points: + uPoints.append(curve.parameter(p)) + for i in range(len(uPoints) - 1): + if ( uNewPoint > uPoints[i] ) and ( uNewPoint < uPoints[i+1] ): + pts.insert(i + 1, edit_command.localize_vector(obj, pt)) + break + # DNC: fix: add points to last segment if curve is closed + if obj.Closed and (uNewPoint > uPoints[-1]): + pts.append(edit_command.localize_vector(obj, pt)) + obj.Points = pts obj.recompute() - return class DraftRectangleGuiTools(GuiTools): @@ -783,29 +766,10 @@ class DraftBezCurveGuiTools(GuiTools): def add_point(self, edit_command, obj, pos): """Add point to obj and reset trackers. """ - # self.setSelectState(obj, True) - selobjs = Gui.ActiveDocument.ActiveView.getObjectsInfo((pos[0],pos[1])) - if not selobjs: + info, pt = edit_command.get_specific_object_info(obj,pos) + if not info or (pt is None): return - for info in selobjs: - if not info: - return - for o in edit_command.edited_objects: - if o.Name != info["Object"]: - continue - obj = o - break - if utils.get_type(obj) == "BezCurve": #to fix double vertex created - # pt = self.point - if "x" in info:# prefer "real" 3D location over working-plane-driven one if possible - pt = App.Vector(info["x"], info["y"], info["z"]) - else: - continue - self.addPointToCurve(pt, obj, info) - obj.recompute() - return - - def addPointToCurve(self, point, obj, info=None): + import Part pts = obj.Points @@ -814,7 +778,7 @@ class DraftBezCurveGuiTools(GuiTools): edgeindex = int(info['Component'].lstrip('Edge')) - 1 wire = obj.Shape.Wires[0] bz = wire.Edges[edgeindex].Curve - param = bz.parameter(point) + param = bz.parameter(pt) seg1 = wire.Edges[edgeindex].copy().Curve seg2 = wire.Edges[edgeindex].copy().Curve seg1.segment(seg1.FirstParameter, param) @@ -839,4 +803,6 @@ class DraftBezCurveGuiTools(GuiTools): obj.Points = pts + obj.recompute() + ## @}