Navigation Menu

Skip to content

Commit

Permalink
Arch: Lightweight mode for Arch References
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Jul 11, 2019
1 parent c79fc5c commit 7eccda3
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 69 deletions.
103 changes: 69 additions & 34 deletions src/Mod/Arch/ArchBuildingPart.py
Expand Up @@ -22,7 +22,14 @@
#* *
#***************************************************************************

import FreeCAD,Draft,ArchCommands,DraftVecUtils,sys,ArchIFC
import FreeCAD
import Draft
import ArchCommands
import DraftVecUtils
import sys
import ArchIFC
import tempfile
import os
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
Expand All @@ -35,7 +42,6 @@ def translate(ctxt,txt):
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
import sys
if sys.version_info.major >= 3:
unicode = str

Expand Down Expand Up @@ -335,6 +341,9 @@ def setProperties(self,obj):
obj.addProperty("App::PropertyString","Tag","Component",QT_TRANSLATE_NOOP("App::Property","An optional tag for this component"))
if not "Shape" in pl:
obj.addProperty("Part::PropertyPartShape","Shape","BuildingPart",QT_TRANSLATE_NOOP("App::Property","The shape of this object"))
if not "SavedInventor" in pl:
obj.addProperty("App::PropertyFileIncluded","SavedInventor","BuildingPart",QT_TRANSLATE_NOOP("App::Property","This property stores an inventor representation for this object"))
obj.setEditorMode("SavedInventor",2)

self.Type = "BuildingPart"

Expand Down Expand Up @@ -368,7 +377,7 @@ def onChanged(self,obj,prop):

elif prop == "Placement":
if hasattr(self,"oldPlacement"):
if self.oldPlacement:
if self.oldPlacement and (self.oldPlacement != obj.Placement):
deltap = obj.Placement.Base.sub(self.oldPlacement.Base)
if deltap.Length == 0:
deltap = None
Expand Down Expand Up @@ -399,8 +408,14 @@ def execute(self,obj):
# gather all the child shapes into a compound
shapes = self.getShapes(obj)
if shapes:
f = []
for s in shapes:
f.extend(s.Faces)
#print("faces before compound:",len(f))
import Part
obj.Shape = Part.makeCompound(shapes)
obj.Shape = Part.makeCompound(f)
#print("faces after compound:",len(obj.Shape.Faces))
#print("recomputing ",obj.Label)
obj.Area = self.getArea(obj)

def getArea(self,obj):
Expand All @@ -421,18 +436,9 @@ def getShapes(self,obj):
"recursively get the shapes of objects inside this BuildingPart"

shapes = []
if obj.isDerivedFrom("Part::Feature") and obj.Shape and (not obj.Shape.isNull()):
shapes.append(obj.Shape)
if hasattr(obj,"Group"):
for child in obj.Group:
shapes.extend(self.getShapes(child))
for i in obj.InList:
if hasattr(i,"Hosts"):
if obj in i.Hosts:
shapes.extend(self.getShapes(i))
elif hasattr(i,"Host"):
if obj == i.Host:
shapes.extend(self.getShapes(i))
for child in Draft.getGroupContents(obj):
if child.isDerivedFrom("Part::Feature"):
shapes.extend(child.Shape.Faces)
return shapes

def getSpaces(self,obj):
Expand Down Expand Up @@ -514,14 +520,15 @@ def setProperties(self,vobj):
vobj.ChildrenLineColor = (float((c>>24)&0xFF)/255.0,float((c>>16)&0xFF)/255.0,float((c>>8)&0xFF)/255.0,0.0)
if not "ChildrenTransparency" in pl:
vobj.addProperty("App::PropertyPercent","ChildrenTransparency","Children",QT_TRANSLATE_NOOP("App::Property","The transparency of child objects"))

if not "CutView" in pl:
vobj.addProperty("App::PropertyBool","CutView","Clip",QT_TRANSLATE_NOOP("App::Property","Cut the view above this level"))
if not "CutMargin" in pl:
vobj.addProperty("App::PropertyLength","CutMargin","Clip",QT_TRANSLATE_NOOP("App::Property","The distance between the level plane and the cut line"))
vobj.CutMargin = 1600
if not "AutoCutView" in pl:
vobj.addProperty("App::PropertyBool","AutoCutView","Clip",QT_TRANSLATE_NOOP("App::Property","Turn cutting on when activating this level"))
if not "SaveInventor" in pl:
vobj.addProperty("App::PropertyBool","SaveInventor","BuildingPart",QT_TRANSLATE_NOOP("App::Property","If this is enabled, the inventor representation of this object will be saved in the FreeCAD file, allowing to reference it in other file sin lightweight mode."))

def onDocumentRestored(self,vobj):

Expand Down Expand Up @@ -591,6 +598,9 @@ def updateData(self,obj,prop):
if len(colors) == len(obj.Shape.Faces):
if colors != obj.ViewObject.DiffuseColor:
obj.ViewObject.DiffuseColor = colors
self.writeInventor(obj)
#else:
#print("color mismatch:",len(colors),"colors,",len(obj.Shape.Faces),"faces")
elif prop == "Group":
self.onChanged(obj.ViewObject,"ChildrenOverride")
elif prop == "Label":
Expand All @@ -601,23 +611,14 @@ def getColors(self,obj):
"recursively get the colors of objects inside this BuildingPart"

colors = []
if obj.isDerivedFrom("Part::Feature") and obj.Shape and (not obj.Shape.isNull()):
if hasattr(obj.ViewObject,"DiffuseColor") and (len(obj.ViewObject.DiffuseColor) == len(obj.Shape.Faces)):
colors.extend(obj.ViewObject.DiffuseColor)
elif hasattr(obj.ViewObject,"ShapeColor"):
c = obj.ViewObject.ShapeColor[:3]+(obj.ViewObject.Transparency/100.0,)
for i in range(len(obj.Shape.Faces)):
colors.append(c)
if hasattr(obj,"Group"):
for child in obj.Group:
colors.extend(self.getColors(child))
for i in obj.InList:
if hasattr(i,"Hosts"):
if obj in i.Hosts:
colors.extend(self.getColors(i))
elif hasattr(i,"Host"):
if obj == i.Host:
colors.extend(self.getColors(i))
for child in Draft.getGroupContents(obj):
if child.isDerivedFrom("Part::Feature"):
if len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
colors.extend(child.ViewObject.DiffuseColor)
else:
c = child.ViewObject.ShapeColor[:3]+(child.ViewObject.Transparency/100.0,)
for i in range(len(child.Shape.Faces)):
colors.append(c)
return colors

def onChanged(self,vobj,prop):
Expand Down Expand Up @@ -728,6 +729,8 @@ def onChanged(self,vobj,prop):
# turn clipping off when turning the object off
if hasattr(vobj,"Visibility") and not(vobj.Visibility) and hasattr(vobj,"CutView"):
vobj.CutView = False
elif prop == "SaveInventor":
self.writeInventor(vobj.Object)

def onDelete(self,vobj,subelements):

Expand All @@ -737,6 +740,7 @@ def onDelete(self,vobj,subelements):
for o in Draft.getGroupContents(vobj.Object.Group,walls=True):
if hasattr(o.ViewObject,"Lighting"):
o.ViewObject.Lighting = "Two side"
return True

def doubleClicked(self,vobj):

Expand Down Expand Up @@ -865,5 +869,36 @@ def __getstate__(self):
def __setstate__(self,state):
return None

def writeInventor(self,obj):

def callback(match):
return next(callback.v)

if hasattr(obj.ViewObject,"SaveInventor") and obj.ViewObject.SaveInventor:
if obj.Shape and obj.Shape.Faces and hasattr(obj,"SavedInventor"):
colors = obj.ViewObject.DiffuseColor
if len(colors) != len(obj.Shape.Faces):
print("Debug: Colors mismatch in",obj.Label)
colors = None
iv = self.Object.Shape.writeInventor()
import re
if colors:
if len(re.findall("IndexedFaceSet",iv)) == len(obj.Shape.Faces):
# convert colors to iv representations
colors = ["Material { diffuseColor "+str(color[0])+" "+str(color[1])+" "+str(color[2])+"}\n IndexedFaceSet" for color in colors]
# replace
callback.v=iter(colors)
iv = re.sub("IndexedFaceSet",callback,iv)
else:
print("Debug: IndexedFaceSet mismatch in",obj.Label)
# save embedded file
tf = tempfile.mkstemp(prefix=obj.Name,suffix=".iv")[1]
f = open(tf,"w")
f.write(iv)
f.close()
obj.SavedInventor = tf
os.remove(tf)


if FreeCAD.GuiUp:
FreeCADGui.addCommand('Arch_BuildingPart',CommandBuildingPart())

0 comments on commit 7eccda3

Please sign in to comment.