[Draft] Improved Autogroup to handle Draft Annotations

Draft Dimension, Label and Text are now correctly auto-added to Part container
This commit is contained in:
carlopav
2020-03-01 17:14:11 +01:00
committed by Yorik van Havre
parent 1d7b62e6ee
commit ff3bdf86bc
2 changed files with 60 additions and 46 deletions

View File

@@ -5274,6 +5274,7 @@ class Draft_Label(Creator):
FreeCAD.ActiveDocument.openTransaction("Create Label") FreeCAD.ActiveDocument.openTransaction("Create Label")
FreeCADGui.addModule("Draft") FreeCADGui.addModule("Draft")
FreeCADGui.doCommand("l = Draft.makeLabel("+tp+sel+"direction='"+direction+"',distance="+str(dist)+",labeltype='"+self.labeltype+"',"+pl+")") FreeCADGui.doCommand("l = Draft.makeLabel("+tp+sel+"direction='"+direction+"',distance="+str(dist)+",labeltype='"+self.labeltype+"',"+pl+")")
FreeCADGui.doCommand("Draft.autogroup(l)")
FreeCAD.ActiveDocument.recompute() FreeCAD.ActiveDocument.recompute()
FreeCAD.ActiveDocument.commitTransaction() FreeCAD.ActiveDocument.commitTransaction()
self.finish() self.finish()

View File

@@ -98,19 +98,20 @@ def autogroup(obj):
obj: App::DocumentObject obj: App::DocumentObject
Any type of object that will be stored in the group. Any type of object that will be stored in the group.
""" """
# check for required conditions for autogroup to work
if not App.GuiUp: if not App.GuiUp:
return return
if not hasattr(Gui,"draftToolBar"):
return
if not hasattr(Gui.draftToolBar,"autogroup"):
return
if Gui.draftToolBar.isConstructionMode():
return
doc = App.ActiveDocument # autogroup code
view = Gui.ActiveDocument.ActiveView
# Look for active Arch container
active_arch_obj = Gui.ActiveDocument.ActiveView.getActiveObject("Arch")
if hasattr(Gui, "draftToolBar"):
if (hasattr(Gui.draftToolBar, "autogroup")
and not Gui.draftToolBar.isConstructionMode()):
if Gui.draftToolBar.autogroup is not None: if Gui.draftToolBar.autogroup is not None:
active_group = doc.getObject(Gui.draftToolBar.autogroup) active_group = App.ActiveDocument.getObject(Gui.draftToolBar.autogroup)
if active_group: if active_group:
found = False found = False
for o in active_group.Group: for o in active_group.Group:
@@ -120,32 +121,44 @@ def autogroup(obj):
gr = active_group.Group gr = active_group.Group
gr.append(obj) gr.append(obj)
active_group.Group = gr active_group.Group = gr
elif active_arch_obj:
active_arch_obj.addObject(obj) else:
elif view.getActiveObject("part", False) is not None:
# Add object to active part and change its placement if Gui.ActiveDocument.ActiveView.getActiveObject("Arch"):
# accordingly so the object does not jump # add object to active Arch Container
# to a different position, works with App::Link if not scaled. Gui.ActiveDocument.ActiveView.getActiveObject("Arch").addObject(obj)
# Modified accordingly to realthunder suggestions
p, parent, sub = view.getActiveObject("part", False) elif Gui.ActiveDocument.ActiveView.getActiveObject("part", False) is not None:
# add object to active part and change it's placement accordingly
# so object does not jump to different position, works with App::Link
# if not scaled. Modified accordingly to realthunder suggestions
p, parent, sub = Gui.ActiveDocument.ActiveView.getActiveObject("part", False)
matrix = parent.getSubObject(sub, retType=4) matrix = parent.getSubObject(sub, retType=4)
if matrix.hasScale() == 1: if matrix.hasScale() == 1:
_msg(translate("Draft", err = translate("Draft",
"Unable to insert new object into " "Unable to insert new object into "
"a scaled part")) "a scaled part")
App.Console.PrintMessage(err)
return return
inverse_placement = App.Placement(matrix.inverse()) inverse_placement = App.Placement(matrix.inverse())
if get_type(obj) == 'Point': if get_type(obj) == 'Point':
# point vector have a kind of placement, so should be
# processed before generic object with placement
point_vector = App.Vector(obj.X, obj.Y, obj.Z) point_vector = App.Vector(obj.X, obj.Y, obj.Z)
real_point = inverse_placement.multVec(point_vector) real_point = inverse_placement.multVec(point_vector)
obj.X = real_point.x obj.X = real_point.x
obj.Y = real_point.y obj.Y = real_point.y
obj.Z = real_point.z obj.Z = real_point.z
elif get_type(obj) in ["Dimension"]:
obj.Start = inverse_placement.multVec(obj.Start)
obj.End = inverse_placement.multVec(obj.End)
obj.Dimline = inverse_placement.multVec(obj.Dimline)
obj.Normal = inverse_placement.Rotation.multVec(obj.Normal)
obj.Direction = inverse_placement.Rotation.multVec(obj.Direction)
elif get_type(obj) in ["Label"]:
obj.Placement = App.Placement(inverse_placement.multiply(obj.Placement))
obj.TargetPoint = inverse_placement.multVec(obj.TargetPoint)
elif hasattr(obj,"Placement"): elif hasattr(obj,"Placement"):
place = inverse_placement.multiply(obj.Placement) # every object that have a placement is processed here
obj.Placement = App.Placement(place) obj.Placement = App.Placement(inverse_placement.multiply(obj.Placement))
p.addObject(obj) p.addObject(obj)