Skip to content

Commit

Permalink
Arch: BuildingParts autogrouping
Browse files Browse the repository at this point in the history
Arch BuildingParts now gained a new set of View properties that allows
to define a "capture box". Subsequent Draft and Arch objects, or anything
else that uses Draft.autogroup(), will be automatically added to that
BuildingPart if they are inside the capture box.
  • Loading branch information
yorikvanhavre committed Oct 14, 2021
1 parent d95f085 commit 0e73140
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/Mod/Arch/ArchBuildingPart.py
Expand Up @@ -411,12 +411,13 @@ def onChanged(self,obj,prop):
print("angle after rotation:",shape.Placement.Rotation.Angle)
child.Placement = shape.Placement
if deltap:
print("moving child")
print("moving child",child.Label)
child.Placement.move(deltap)

def execute(self,obj):

# gather all the child shapes into a compound
pl = obj.Placement
shapes,materialstable = self.getShapes(obj)
if shapes:
import Part
Expand All @@ -430,8 +431,12 @@ def execute(self,obj):
#print("recomputing ",obj.Label)
else:
obj.Shape = Part.makeCompound(shapes)
obj.Placement = pl
obj.Area = self.getArea(obj)
obj.MaterialsTable = materialstable
if obj.ViewObject:
# update the autogroup box if needed
obj.ViewObject.Proxy.onChanged(obj.ViewObject,"AutoGroupBox")

def getArea(self,obj):

Expand Down Expand Up @@ -498,6 +503,28 @@ def addObject(self,obj,child):
g.append(child)
obj.Group = g

def autogroup(self,obj,child):

"Adds an object to the group of this BuildingPart automatically"

if obj.ViewObject:
if hasattr(obj.ViewObject.Proxy,"autobbox") and obj.ViewObject.Proxy.autobbox:
if hasattr(child,"Shape") and child.Shape:
abb = obj.ViewObject.Proxy.autobbox
cbb = child.Shape.BoundBox
if abb.isValid():
if not cbb.isValid():
FreeCAD.ActiveDocument.recompute()
if not cbb.isValid():
cbb = FreeCAD.BoundBox()
for v in child.Shape.Vertexes:
print(v.Point)
cbb.add(v.Point)
if cbb.isValid() and abb.isInside(cbb):
self.addObject(obj,child)
return True
return False


class ViewProviderBuildingPart:

Expand Down Expand Up @@ -589,6 +616,16 @@ def setProperties(self,vobj):
if not "AutoCutView" in pl:
vobj.addProperty("App::PropertyBool","AutoCutView","Clip",QT_TRANSLATE_NOOP("App::Property","Turn cutting on when activating this level"))

# autogroup properties
if not "AutogroupSize" in pl:
vobj.addProperty("App::PropertyIntegerList","AutogroupSize","AutoGroup",QT_TRANSLATE_NOOP("App::Property","The capture box for newly created objects expressed as [XMin,YMin,ZMin,XMax,YMax,ZMax]"))
if not "AutogroupBox" in pl:
vobj.addProperty("App::PropertyBool","AutogroupBox","AutoGroup",QT_TRANSLATE_NOOP("App::Property","Turns auto group box on/off"))
if not "AutogroupAutosize" in pl:
vobj.addProperty("App::PropertyBool","AutogroupAutosize","AutoGroup",QT_TRANSLATE_NOOP("App::Property","Automatically set size from contents"))
if not "AutogroupMargin" in pl:
vobj.addProperty("App::PropertyLength","AutogroupMargin","AutoGroup",QT_TRANSLATE_NOOP("App::Property","A margin to use when autosize is turned on"))

def onDocumentRestored(self,vobj):

self.setProperties(vobj)
Expand Down Expand Up @@ -618,6 +655,21 @@ def attach(self,vobj):
lin = coin.SoType.fromName("SoBrepEdgeSet").createInstance()
lin.coordIndex.setValues([0,1,-1,2,3,-1,4,5,-1])
self.sep.addChild(lin)
self.bbox = coin.SoSwitch()
self.bbox.whichChild = -1
bboxsep = coin.SoSeparator()
self.bbox.addChild(bboxsep)
drawstyle = coin.SoDrawStyle()
drawstyle.style = coin.SoDrawStyle.LINES
drawstyle.lineWidth = 3
drawstyle.linePattern = 0x0f0f # 0xaa
bboxsep.addChild(drawstyle)
self.bbco = coin.SoCoordinate3()
bboxsep.addChild(self.bbco)
lin = coin.SoIndexedLineSet()
lin.coordIndex.setValues([0,1,2,3,0,-1,4,5,6,7,4,-1,0,4,-1,1,5,-1,2,6,-1,3,7,-1])
bboxsep.addChild(lin)
self.sep.addChild(self.bbox)
self.tra = coin.SoTransform()
self.tra.rotation.setValue(FreeCAD.Rotation(0,0,90).Q)
self.sep.addChild(self.tra)
Expand All @@ -632,6 +684,8 @@ def attach(self,vobj):
self.onChanged(vobj,"FontName")
self.onChanged(vobj,"ShowLevel")
self.onChanged(vobj,"FontSize")
self.onChanged(vobj,"AutogroupBox")
self.setProperties(vobj)
return

def getDisplayModes(self,vobj):
Expand Down Expand Up @@ -793,6 +847,23 @@ def onChanged(self,vobj,prop):
vobj.CutView = False
elif prop == "SaveInventor":
self.writeInventor(vobj.Object)
elif prop in ["AutogroupBox","AutogroupSize"]:
if hasattr(vobj,"AutogroupBox") and hasattr(vobj,"AutogroupSize"):
if vobj.AutogroupBox:
if len(vobj.AutogroupSize) >= 6:
self.autobbox = FreeCAD.BoundBox(*vobj.AutogroupSize[0:6])
self.autobbox.move(vobj.Object.Placement.Base)
pts = [list(self.autobbox.getPoint(i)) for i in range(8)]
self.bbco.point.setValues(pts)
self.bbox.whichChild = 0
else:
self.autobbox = None
self.bbox.whichChild = -1
elif prop in ["AutogroupAutosize","AutogroupMargin"]:
if hasattr(vobj,"AutogroupAutosize") and vobj.AutogroupAutosize:
bbox = vobj.Object.Shape.BoundBox
bbox.enlarge(vobj.AutogroupMargin.Value)
vobj.AutogroupSize = [int(i) for i in [bbox.XMin,bbox.YMin,bbox.ZMin,bbox.XMax,bbox.YMax,bbox.ZMax]]

def onDelete(self,vobj,subelements):

Expand Down
7 changes: 7 additions & 0 deletions src/Mod/Draft/draftutils/gui_utils.py
Expand Up @@ -115,6 +115,13 @@ def autogroup(obj):
if Gui.draftToolBar.isConstructionMode():
return

# check first for objects that do autogroup themselves
# at the moment only Arch_BuildingPart, which is an App::GeometryPython
for par in App.ActiveDocument.findObjects(Type="App::GeometryPython"):
if hasattr(par.Proxy,"autogroup"):
if par.Proxy.autogroup(par,obj):
return

# autogroup code
if Gui.draftToolBar.autogroup is not None:
active_group = App.ActiveDocument.getObject(Gui.draftToolBar.autogroup)
Expand Down

0 comments on commit 0e73140

Please sign in to comment.