Arch: massive rewrite of extrusion-based Arch objects and IFC export

This commit is contained in:
Yorik van Havre 2016-10-27 15:08:16 -02:00
parent bd8a296be6
commit 65aaf16201
8 changed files with 666 additions and 622 deletions

View File

@ -432,14 +432,25 @@ def getShapeFromMesh(mesh,fast=True,tolerance=0.001,flat=False,cut=True):
try:
f = Part.Face(Part.makePolygon(pts))
except:
pass
print "getShapeFromMesh: error building face from polygon"
#pass
else:
faces.append(f)
shell = Part.makeShell(faces)
try:
solid = Part.Solid(shell)
except Part.OCCError:
print "getShapeFromMesh: error creating solid"
else:
try:
solid = solid.removeSplitter()
except Part.OCCError:
print "getShapeFromMesh: error removing splitter"
#pass
return solid
#if not mesh.isSolid():
# print "getShapeFromMesh: non-solid mesh, using slow method"
faces = []
segments = mesh.getPlanarSegments(tolerance)
#print len(segments)
@ -462,9 +473,11 @@ def getShapeFromMesh(mesh,fast=True,tolerance=0.001,flat=False,cut=True):
if flat:
return se
except Part.OCCError:
print "getShapeFromMesh: error removing splitter"
try:
cp = Part.makeCompound(faces)
except Part.OCCError:
print "getShapeFromMesh: error creating compound"
return None
else:
return cp
@ -472,6 +485,7 @@ def getShapeFromMesh(mesh,fast=True,tolerance=0.001,flat=False,cut=True):
try:
solid = Part.Solid(se)
except Part.OCCError:
print "getShapeFromMesh: error creating solid"
return se
else:
return solid
@ -673,7 +687,7 @@ def pruneIncluded(objectslist,strict=False):
if obj.isDerivedFrom("Part::Feature"):
if not (Draft.getType(obj) in ["Window","Clone","Pipe"]):
for parent in obj.InList:
if parent.isDerivedFrom("Part::Feature"):
if parent.isDerivedFrom("Part::Feature") and not (Draft.getType(parent) in ["Facebinder"]):
if not parent.isDerivedFrom("Part::Part2DObject"):
# don't consider 2D objects based on arch elements
if hasattr(parent,"CloneOf"):
@ -936,8 +950,12 @@ def getExtrusionData(shape):
return None
# build faces list with normals
faces = []
import Part
for f in shape.Faces:
try:
faces.append([f,f.normalAt(0,0)])
except Part.OCCError:
return None
# find opposite normals pairs
pairs = []
for i1, f1 in enumerate(faces):

View File

@ -31,7 +31,7 @@ Roles = ['Undefined','Beam','Chimney','Column','Covering','Curtain Wall',
'Member','Plate','Railing','Ramp','Ramp Flight','Rebar','Pile','Roof','Shading Device','Slab','Space',
'Stair','Stair Flight','Tendon','Wall','Wall Layer','Window']
import FreeCAD,Draft,ArchCommands
import FreeCAD,Draft,ArchCommands,math
from FreeCAD import Vector
if FreeCAD.GuiUp:
import FreeCADGui
@ -363,195 +363,43 @@ class Component:
siblings.append(o)
return siblings
def getAxis(self,obj):
"Returns an open wire which is the axis of this component, if applicable"
if Draft.getType(obj) == "Precast":
return None
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape:
if (len(obj.Base.Shape.Wires) == 1) and not(obj.Base.Shape.Faces):
if not obj.Base.Shape.Wires[0].isClosed():
return obj.Base.Shape.copy()
elif not(obj.Base.Shape.Solids):
if hasattr(obj.Base.Shape,"CenterOfMass"):
p1 = obj.Base.Shape.CenterOfMass
v = self.getExtrusionVector(obj)
if v:
p2 = p1.add(v)
import Part
return Part.Line(p1,p2).toShape()
else:
p1 = FreeCAD.Vector()
v = self.getExtrusionVector(obj)
if v:
p2 = p1.add(v)
import Part
return Part.Line(p1,p2).toShape()
return None
def getProfiles(self,obj,noplacement=False):
"Returns the base profile(s) of this component, if applicable"
wires = []
if Draft.getType(obj) == "Precast":
return wires
n,l,w,h = self.getDefaultValues(obj)
def getExtrusionData(self,obj):
"returns (shape,extrusion vector,placement) or None"
if hasattr(obj,"CloneOf"):
if obj.CloneOf:
data = obj.CloneOf.Proxy.getExtrusionData(obj.CloneOf)
if data:
return data
if obj.Base:
if obj.Base.isDerivedFrom("Part::Extrusion"):
if obj.Base.Base:
base = obj.Base.Base.Shape.copy()
#if noplacement:
# base.Placement = FreeCAD.Placement()
return [base]
elif obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape:
base = obj.Base.Shape.copy()
if noplacement:
base.Placement = FreeCAD.Placement()
if not base.Solids:
if base.Faces:
import DraftGeomUtils
if not DraftGeomUtils.isCoplanar(base.Faces):
return []
return [base]
base,placement = self.rebase(obj.Base.Base.Shape)
extrusion = obj.Base.Dir
if extrusion.Length == 0:
extrusion = FreeCAD.Vector(0,0,1)
if hasattr(obj.Base,"LengthForward"):
if obj.Base.LengthForward.Value:
extrusion = extrusion.multiply(obj.Base.LengthForward.Value)
return (base,extrusion,placement)
return None
basewires = []
if not base.Wires:
if len(base.Edges) == 1:
import Part
basewires = [Part.Wire(base.Edges)]
def rebase(self,shape):
import DraftGeomUtils,math
if hasattr(shape,"CenterOfMass"):
v = shape.CenterOfMass
else:
basewires = base.Wires
if basewires:
import DraftGeomUtils,DraftVecUtils,Part
for wire in basewires:
e = wire.Edges[0]
if isinstance(e.Curve,Part.Circle):
dvec = e.Vertexes[0].Point.sub(e.Curve.Center)
else:
dvec = DraftGeomUtils.vec(wire.Edges[0]).cross(n)
if not DraftVecUtils.isNull(dvec):
dvec.normalize()
sh = None
if hasattr(obj,"Align"):
if obj.Align == "Left":
dvec.multiply(w)
if hasattr(obj,"Offset"):
if obj.Offset.Value:
dvec2 = DraftVecUtils.scaleTo(dvec,obj.Offset.Value)
wire = DraftGeomUtils.offsetWire(wire,dvec2)
w2 = DraftGeomUtils.offsetWire(wire,dvec)
w1 = Part.Wire(Part.__sortEdges__(wire.Edges))
sh = DraftGeomUtils.bind(w1,w2)
elif obj.Align == "Right":
dvec.multiply(w)
dvec = dvec.negative()
if hasattr(obj,"Offset"):
if obj.Offset.Value:
dvec2 = DraftVecUtils.scaleTo(dvec,obj.Offset.Value)
wire = DraftGeomUtils.offsetWire(wire,dvec2)
w2 = DraftGeomUtils.offsetWire(wire,dvec)
w1 = Part.Wire(Part.__sortEdges__(wire.Edges))
sh = DraftGeomUtils.bind(w1,w2)
elif obj.Align == "Center":
dvec.multiply(w/2)
w1 = DraftGeomUtils.offsetWire(wire,dvec)
dvec = dvec.negative()
w2 = DraftGeomUtils.offsetWire(wire,dvec)
sh = DraftGeomUtils.bind(w1,w2)
if sh:
wires.append(sh)
else:
wires.append(wire)
elif Draft.getType(obj) in ["Wall","Structure"]:
if (Draft.getType(obj) == "Structure") and (l > h) and (obj.Role != "Slab"):
if noplacement:
h2 = h/2 or 0.5
w2 = w/2 or 0.5
v1 = Vector(-h2,-w2,0)
v2 = Vector(h2,-w2,0)
v3 = Vector(h2,w2,0)
v4 = Vector(-h2,w2,0)
else:
h2 = h/2 or 0.5
w2 = w/2 or 0.5
v1 = Vector(0,-w2,-h2)
v2 = Vector(0,-w2,h2)
v3 = Vector(0,w2,h2)
v4 = Vector(0,w2,-h2)
else:
l2 = l/2 or 0.5
w2 = w/2 or 0.5
v1 = Vector(-l2,-w2,0)
v2 = Vector(l2,-w2,0)
v3 = Vector(l2,w2,0)
v4 = Vector(-l2,w2,0)
import Part
base = Part.makePolygon([v1,v2,v3,v4,v1])
return [base]
return wires
def getExtrusionVector(self,obj,noplacement=False):
"Returns an extrusion vector of this component, if applicable"
n,l,w,h = self.getDefaultValues(obj)
if Draft.getType(obj) == "Precast":
return FreeCAD.Vector()
if obj.Base:
if obj.Base.isDerivedFrom("Part::Extrusion"):
return FreeCAD.Vector(obj.Base.Dir)
if Draft.getType(obj) == "Structure":
if l > h:
v = n.multiply(l)
if noplacement:
import DraftVecUtils
v = DraftVecUtils.rounded(FreeCAD.Rotation(FreeCAD.Vector(0,1,0),-90).multVec(v))
return v
return n.multiply(h)
def getDefaultValues(self,obj):
"returns normal,length,width,height values from this component"
length = 0
if hasattr(obj,"Length"):
if obj.Length.Value:
length = obj.Length.Value
width = 0
if hasattr(obj,"Width"):
if obj.Width.Value:
width = obj.Width.Value
height = 0
if hasattr(obj,"Height"):
if obj.Height.Value:
height = obj.Height.Value
else:
for p in obj.InList:
if Draft.getType(p) == "Floor":
if p.Height.Value:
height = p.Height.Value
default = Vector(0,0,1)
if Draft.getType(obj) == "Structure":
if length > height:
default = Vector(1,0,0)
if hasattr(obj,"Normal"):
if obj.Normal == Vector(0,0,0):
normal = default
else:
normal = Vector(obj.Normal)
else:
normal = default
return normal,length,width,height
def getPlacement(self,obj):
"returns a total placement for the profile of this component"
v = shape.BoundBox.Center
n = DraftGeomUtils.getNormal(shape)
r = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),n)
if round(r.Angle,8) == round(math.pi,8):
r = FreeCAD.Rotation()
shape = shape.copy()
shape.translate(v.negative())
shape.rotate(FreeCAD.Vector(0,0,0),r.inverted().Axis,math.degrees(r.inverted().Angle))
p = FreeCAD.Placement()
if obj.Base:
p = obj.Base.Placement.multiply(p)
else:
if Draft.getType(obj) == "Structure":
n,l,w,h = self.getDefaultValues(obj)
if l > h:
p.Rotation = FreeCAD.Rotation(FreeCAD.Vector(0,1,0),90)
p = obj.Placement.multiply(p)
return p
p.Base = v
p.Rotation = r
return (shape,p)
def hideSubobjects(self,obj,prop):
"Hides subobjects when a subobject lists change"
@ -701,10 +549,16 @@ class Component:
return
if not obj.Shape.Faces:
return
import Drawing,Part
a = 0
fset = []
for f in obj.Shape.Faces:
for i,f in enumerate(obj.Shape.Faces):
try:
ang = f.normalAt(0,0).getAngle(FreeCAD.Vector(0,0,1))
except Part.OCCError:
print "Debug: Error computing areas for ",obj.Label,": normalAt() Face ",i
return
else:
if (ang > 1.57) and (ang < 1.571):
a += f.Area
if ang < 1.5707:
@ -713,15 +567,18 @@ class Component:
if obj.VerticalArea.Value != a:
obj.VerticalArea = a
if fset and hasattr(obj,"HorizontalArea"):
import Drawing,Part
pset = []
for f in fset:
if f.normalAt(0,0).getAngle(FreeCAD.Vector(0,0,1)) < 0.00001:
# already horizontal
pset.append(f)
else:
try:
pf = Part.Face(Part.Wire(Drawing.project(f,FreeCAD.Vector(0,0,1))[0].Edges))
except Part.OCCError:
# error in computing the areas. Better set them to zero than show a wrong value
if obj.HorizontalArea.Value != 0:
print "Error computing areas for ",obj.Label
print "Debug: Error computing areas for ",obj.Label,": unable to project face: ",str([v.Point for v in f.Vertexes])," (face normal:",f.normalAt(0,0),")"
obj.HorizontalArea = 0
if hasattr(obj,"PerimeterLength"):
if obj.PerimeterLength.Value != 0:

View File

@ -162,6 +162,7 @@ class _ArchPipe(ArchComponent.Component):
ArchComponent.Component.__init__(self,obj)
self.Type = "Pipe"
obj.Role = ["Pipe Segment"]
obj.addProperty("App::PropertyLength", "Diameter", "Arch", QT_TRANSLATE_NOOP("App::Property","The diameter of this pipe, if not based on a profile"))
obj.addProperty("App::PropertyLength", "Length", "Arch", QT_TRANSLATE_NOOP("App::Property","The length of this pipe, if not based on an edge"))
obj.addProperty("App::PropertyLink", "Profile", "Arch", QT_TRANSLATE_NOOP("App::Property","An optional closed profile to base this pipe on"))
@ -275,6 +276,7 @@ class _ArchPipeConnector(ArchComponent.Component):
ArchComponent.Component.__init__(self,obj)
self.Type = "PipeConnector"
obj.Role = ["Pipe Fitting"]
obj.addProperty("App::PropertyLength", "Radius", "Arch", QT_TRANSLATE_NOOP("App::Property","The curvature radius of this connector"))
obj.addProperty("App::PropertyLinkList", "Pipes", "Arch", QT_TRANSLATE_NOOP("App::Property","The pipes linked by this connector"))
obj.addProperty("App::PropertyEnumeration", "ConnectorType", "Arch", QT_TRANSLATE_NOOP("App::Property","The type of this connector"))

View File

@ -56,12 +56,6 @@ class _Precast(ArchComponent.Component):
self.Type = "Precast"
obj.Role = ["Beam","Column","Panel","Slab","Stairs"]
def getProfile(self,obj,noplacement=True):
return []
def getExtrusionVector(self,obj,noplacement=True):
return FreeCAD.Vector()
def execute(self,obj):
if self.clone(obj):

View File

@ -307,7 +307,7 @@ class _Space(ArchComponent.Component):
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape.Solids:
shape = obj.Base.Shape.Solids[0].copy()
shape = obj.Base.Shape.copy()
shape = shape.removeSplitter()
# 2: if not, add all bounding boxes of considered objects and build a first shape

View File

@ -374,7 +374,9 @@ class _CommandStructure:
class _Structure(ArchComponent.Component):
"The Structure object"
def __init__(self,obj):
ArchComponent.Component.__init__(self,obj)
obj.addProperty("App::PropertyLink","Tool","Arch",QT_TRANSLATE_NOOP("App::Property","An optional extrusion path for this element"))
@ -386,7 +388,9 @@ class _Structure(ArchComponent.Component):
obj.addProperty("App::PropertyVectorList","Nodes","Arch",QT_TRANSLATE_NOOP("App::Property","The structural nodes of this element"))
obj.addProperty("App::PropertyString","Profile","Arch",QT_TRANSLATE_NOOP("App::Property","A description of the standard profile this element is based upon"))
obj.addProperty("App::PropertyDistance","NodesOffset","Arch",QT_TRANSLATE_NOOP("App::Property","Offset distance between the centerline and the nodes line"))
obj.addProperty("App::PropertyEnumeration","FaceMaker","Arch",QT_TRANSLATE_NOOP("App::Property","The facemaker type to use to build the profile of this object"))
self.Type = "Structure"
obj.FaceMaker = ["None","Simple","Cheese","Bullseye"]
obj.Role = Roles
def execute(self,obj):
@ -397,11 +401,22 @@ class _Structure(ArchComponent.Component):
if self.clone(obj):
return
normal,length,width,height = self.getDefaultValues(obj)
# creating base shape
pl = obj.Placement
import Part, DraftGeomUtils
base = None
pl = obj.Placement
extdata = self.getExtrusionData(obj)
if extdata:
base = extdata[0]
base.Placement = extdata[2].multiply(base.Placement)
extv = extdata[2].Rotation.multVec(extdata[1])
if obj.Tool:
try:
base = obj.Tool.Shape.copy().makePipe(obj.Base.Shape.copy())
except Part.OCCError:
FreeCAD.Console.PrintError(translate("Arch","Error: The base shape couldn't be extruded along this tool object")+"\n")
return
else:
base = base.extrude(extv)
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape.isNull():
@ -410,42 +425,8 @@ class _Structure(ArchComponent.Component):
if not obj.Base.Shape.Solids:
# let pass invalid objects if they have solids...
return
if hasattr(obj,"Tool"):
if obj.Tool:
try:
base = obj.Tool.Shape.copy().makePipe(obj.Base.Shape.copy())
except Part.OCCError:
FreeCAD.Console.PrintError(translate("Arch","Error: The base shape couldn't be extruded along this tool object"))
return
if not base:
if not height:
return
if obj.Normal == Vector(0,0,0):
if len(obj.Base.Shape.Faces) > 0 :
normal=obj.Base.Shape.Faces[0].normalAt(.5,.5)
else:
normal = DraftGeomUtils.getNormal(obj.Base.Shape)
if not normal:
normal = FreeCAD.Vector(0,0,1)
#p = FreeCAD.Placement(obj.Base.Placement)
#normal = p.Rotation.multVec(normal)
else:
normal = Vector(obj.Normal)
normal = normal.multiply(height)
elif obj.Base.Shape.Solids:
base = obj.Base.Shape.copy()
if base.Solids:
pass
elif base.Faces:
base = base.extrude(normal)
elif (len(base.Wires) == 1):
if base.Wires[0].isClosed():
try:
base = Part.Face(base.Wires[0])
base = base.extrude(normal)
except Part.OCCError:
FreeCAD.Console.PrintError(obj.Label+" : "+str(translate("Arch","Unable to extrude the base shape\n")))
return
elif obj.Base.isDerivedFrom("Mesh::Feature"):
if obj.Base.Mesh.isSolid():
if obj.Base.Mesh.countComponents() == 1:
@ -453,27 +434,114 @@ class _Structure(ArchComponent.Component):
if sh.isClosed() and sh.isValid() and sh.Solids and (not sh.isNull()):
base = sh
else:
FreeCAD.Console.PrintWarning(str(translate("Arch","This mesh is an invalid solid")))
FreeCAD.Console.PrintWarning(translate("Arch","This mesh is an invalid solid")+"\n")
obj.Base.ViewObject.show()
else:
base = self.getProfiles(obj)
if base:
if length > height:
normal = normal.multiply(length)
else:
normal = normal.multiply(height)
base = Part.Face(base[0])
base = base.extrude(normal)
if not base:
FreeCAD.Console.PrintError(translate("Arch","Error: Invalid base object")+"\n")
return
base = self.processSubShapes(obj,base,pl)
self.applyShape(obj,base,pl)
def getExtrusionData(self,obj):
"""returns (shape,extrusion vector,placement) or None"""
import Part,DraftGeomUtils
data = ArchComponent.Component.getExtrusionData(self,obj)
if data:
return data
length = obj.Length.Value
width = obj.Width.Value
height = obj.Height.Value
normal = None
if not height:
for p in obj.InList:
if Draft.getType(p) == "Floor":
if p.Height.Value:
height = p.Height.Value
base = None
placement = None
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape:
if obj.Base.Shape.Solids:
return None
elif obj.Base.Shape.Faces:
if not DraftGeomUtils.isCoplanar(obj.Base.Shape.Faces):
return None
else:
base,placement = self.rebase(obj.Base.Shape)
normal = obj.Base.Shape.Faces[0].normalAt(0,0)
elif obj.Base.Shape.Wires:
baseface = None
if hasattr(obj,"FaceMaker"):
if obj.FaceMaker != "None":
try:
baseface = Part.makeFace(obj.Base.Shape.Wires,"Part::FaceMaker"+str(obj.FaceMaker))
except:
FreeCAD.Console.PrintError(translate("Arch","Facemaker returned an error")+"\n")
return None
normal = baseface.normalAt(0,0)
if not baseface:
for w in obj.Base.Shape.Wires:
w.fix(0.1,0,1) # fixes self-intersecting wires
f = Part.Face(sh)
if baseface:
baseface = baseface.fuse(f)
else:
baseface = f
normal = f.normalAt(0,0)
base,placement = self.rebase(baseface)
elif (len(obj.Base.Shape.Edges) == 1) and (len(obj.Base.Shape.Vertexes) == 1):
# closed edge
w = Part.Wire(obj.Base.Shape.Edges[0])
baseface = Part.Face(w)
base,placement = self.rebase(baseface)
elif length and width and height:
if (length > height) and (obj.Role != "Slab"):
h2 = height/2 or 0.5
w2 = width/2 or 0.5
v1 = Vector(0,-w2,-h2)
v2 = Vector(0,-w2,h2)
v3 = Vector(0,w2,h2)
v4 = Vector(0,w2,-h2)
else:
l2 = length/2 or 0.5
w2 = width/2 or 0.5
v1 = Vector(-l2,-w2,0)
v2 = Vector(l2,-w2,0)
v3 = Vector(l2,w2,0)
v4 = Vector(-l2,w2,0)
import Part
baseface = Part.Face(Part.makePolygon([v1,v2,v3,v4,v1]))
base,placement = self.rebase(baseface)
if base and placement:
if obj.Normal == Vector(0,0,0):
if not normal:
normal = Vector(0,0,1)
else:
normal = Vector(obj.Normal)
if (length > height) and (obj.Role != "Slab"):
extrusion = normal.multiply(length)
else:
extrusion = normal.multiply(height)
return (base,extrusion,placement)
return None
def onChanged(self,obj,prop):
self.hideSubobjects(obj,prop)
if prop in ["Shape","ResetNodes","NodesOffset"]:
# ResetNodes is not a property but it allows us to use this function to force reset the nodes
if hasattr(obj,"Nodes"):
# update structural nodes
nodes = None
extdata = self.getExtrusionData(obj)
if extdata:
nodes = extdata[0]
nodes.Placement = nodes.Placement.multiply(extdata[2])
if obj.Role not in ["Slab"]:
if obj.Tool:
nodes = obj.Tool.Shape
else:
import Part
nodes = Part.Line(nodes.CenterOfMass,nodes.CenterOfMass.add(extdata[1])).toShape()
offset = FreeCAD.Vector()
if hasattr(obj,"NodesOffset"):
offset = FreeCAD.Vector(0,0,obj.NodesOffset.Value)
@ -485,19 +553,11 @@ class _Structure(ArchComponent.Component):
return
else:
# nodes haven't been calculated yet, but are set (file load)
# we calculate the nodes now but don't change the property
if obj.Role in ["Slab"]:
nodes = self.getProfiles(obj)[0]
else:
nodes = self.getAxis(obj)
# we set the nodes now but don't change the property
if nodes:
self.nodes = [v.Point.add(offset) for v in nodes.Vertexes]
return
# we calculate and set the nodes
if obj.Role in ["Slab"]:
nodes = self.getProfiles(obj)[0]
else:
nodes = self.getAxis(obj)
# we set the nodes
if nodes:
self.nodes = [v.Point.add(offset) for v in nodes.Vertexes]
obj.Nodes = self.nodes

View File

@ -429,13 +429,15 @@ class _Wall(ArchComponent.Component):
return
import Part, DraftGeomUtils
pl = obj.Placement
normal,length,width,height = self.getDefaultValues(obj)
base = None
face = None
pl = obj.Placement
extdata = self.getExtrusionData(obj)
if extdata:
base = extdata[0]
base.Placement = extdata[2].multiply(base.Placement)
extv = extdata[2].Rotation.multVec(extdata[1])
base = base.extrude(extv)
if obj.Base:
# computing a shape from a base object
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape.isNull():
return
@ -443,51 +445,8 @@ class _Wall(ArchComponent.Component):
if not obj.Base.Shape.Solids:
# let pass invalid objects if they have solids...
return
if hasattr(obj,"Face"):
if obj.Face > 0:
if len(obj.Base.Shape.Faces) >= obj.Face:
face = obj.Base.Shape.Faces[obj.Face-1]
if face:
# case 1: this wall is based on a specific face of its base object
normal = face.normalAt(0,0)
if normal.getAngle(Vector(0,0,1)) > math.pi/4:
normal.multiply(width)
base = face.extrude(normal)
if obj.Align == "Center":
base.translate(normal.negative().multiply(0.5))
elif obj.Align == "Right":
base.translate(normal.negative())
else:
normal.multiply(height)
base = face.extrude(normal)
elif obj.Base.Shape.Solids:
# case 2: the base is already a solid
base = obj.Base.Shape.copy()
elif obj.Base.Shape.Edges:
# case 3: the base is flat, we need to extrude it
if not obj.Base.Shape.Faces:
# set the length property
if hasattr(obj.Base.Shape,"Length"):
l = obj.Base.Shape.Length
if obj.Length != l:
obj.Length = l
profiles = self.getProfiles(obj)
if profiles:
normal.multiply(height)
base = profiles.pop()
base.fix(0.1,0,1)
base = base.extrude(normal)
for p in profiles:
p.fix(0.1,0,1)
p = p.extrude(normal)
base = base.fuse(p)
else:
base = None
else:
base = None
FreeCAD.Console.PrintError(str(translate("Arch","Error: Invalid base object")))
elif obj.Base.isDerivedFrom("Mesh::Feature"):
if obj.Base.Mesh.isSolid():
if obj.Base.Mesh.countComponents() == 1:
@ -495,16 +454,25 @@ class _Wall(ArchComponent.Component):
if sh.isClosed() and sh.isValid() and sh.Solids and (not sh.isNull()):
base = sh
else:
FreeCAD.Console.PrintWarning(str(translate("Arch","This mesh is an invalid solid")))
FreeCAD.Console.PrintWarning(translate("Arch","This mesh is an invalid solid")+"\n")
obj.Base.ViewObject.show()
else:
# computing a shape from scratch
if length and width and height:
base = Part.makeBox(length,width,height)
if not base:
FreeCAD.Console.PrintError(translate("Arch","Error: Invalid base object")+"\n")
return
base = self.processSubShapes(obj,base,pl)
self.applyShape(obj,base,pl)
# set the length property
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape.Edges:
if not obj.Base.Shape.Faces:
if hasattr(obj.Base.Shape,"Length"):
l = obj.Base.Shape.Length
if obj.Length.Value != l:
obj.Length = l
def onChanged(self,obj,prop):
self.hideSubobjects(obj,prop)
ArchComponent.Component.onChanged(self,obj,prop)
@ -518,6 +486,114 @@ class _Wall(ArchComponent.Component):
faces.append(f)
return faces
def getExtrusionData(self,obj):
"""returns (shape,extrusion vector,placement) or None"""
import Part,DraftGeomUtils
data = ArchComponent.Component.getExtrusionData(self,obj)
if data:
return data
length = obj.Length.Value
width = obj.Width.Value
height = obj.Height.Value
if not height:
for p in obj.InList:
if Draft.getType(p) == "Floor":
if p.Height.Value:
height = p.Height.Value
if obj.Normal == Vector(0,0,0):
normal = Vector(0,0,1)
else:
normal = Vector(obj.Normal)
base = None
placement = None
basewires = None
if obj.Base:
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape:
if obj.Base.Shape.Solids:
return None
elif obj.Face > 0:
if len(obj.Base.Shape.Faces) >= obj.Face:
face = obj.Base.Shape.Faces[obj.Face-1]
# this wall is based on a specific face of its base object
normal = face.normalAt(0,0)
if normal.getAngle(Vector(0,0,1)) > math.pi/4:
normal.multiply(width)
base = face.extrude(normal)
if obj.Align == "Center":
base.translate(normal.negative().multiply(0.5))
elif obj.Align == "Right":
base.translate(normal.negative())
else:
normal.multiply(height)
base = face.extrude(normal)
base,placement = self.rebase(base)
return (base,normal,placement)
elif obj.Base.Shape.Faces:
if not DraftGeomUtils.isCoplanar(obj.Base.Shape.Faces):
return None
else:
base,placement = self.rebase(obj.Base.Shape)
elif obj.Base.Shape.Wires:
basewires = obj.Base.Shape.Wires
elif len(obj.Base.Shape.Edges) == 1:
basewires = [Part.Wire(obj.Base.Shape.Edges)]
if basewires and width:
baseface = None
for wire in basewires:
e = wire.Edges[0]
if isinstance(e.Curve,Part.Circle):
dvec = e.Vertexes[0].Point.sub(e.Curve.Center)
else:
dvec = DraftGeomUtils.vec(wire.Edges[0]).cross(normal)
if not DraftVecUtils.isNull(dvec):
dvec.normalize()
sh = None
if obj.Align == "Left":
dvec.multiply(width)
if obj.Offset.Value:
dvec2 = DraftVecUtils.scaleTo(dvec,obj.Offset.Value)
wire = DraftGeomUtils.offsetWire(wire,dvec2)
w2 = DraftGeomUtils.offsetWire(wire,dvec)
w1 = Part.Wire(Part.__sortEdges__(wire.Edges))
sh = DraftGeomUtils.bind(w1,w2)
elif obj.Align == "Right":
dvec.multiply(width)
dvec = dvec.negative()
if obj.Offset.Value:
dvec2 = DraftVecUtils.scaleTo(dvec,obj.Offset.Value)
wire = DraftGeomUtils.offsetWire(wire,dvec2)
w2 = DraftGeomUtils.offsetWire(wire,dvec)
w1 = Part.Wire(Part.__sortEdges__(wire.Edges))
sh = DraftGeomUtils.bind(w1,w2)
elif obj.Align == "Center":
dvec.multiply(width/2)
w1 = DraftGeomUtils.offsetWire(wire,dvec)
dvec = dvec.negative()
w2 = DraftGeomUtils.offsetWire(wire,dvec)
sh = DraftGeomUtils.bind(w1,w2)
if sh:
sh.fix(0.1,0,1) # fixes self-intersecting wires
f = Part.Face(sh)
if baseface:
baseface = baseface.fuse(f)
else:
baseface = f
if baseface:
base,placement = self.rebase(baseface)
else:
l2 = length/2 or 0.5
w2 = width/2 or 0.5
v1 = Vector(-l2,-w2,0)
v2 = Vector(l2,-w2,0)
v3 = Vector(l2,w2,0)
v4 = Vector(-l2,w2,0)
base = Part.Face(Part.makePolygon([v1,v2,v3,v4,v1]))
placement = FreeCAD.Placement()
if base and placement:
extrusion = normal.multiply(height)
return (base,extrusion,placement)
return None
class _ViewProviderWall(ArchComponent.ViewProviderComponent):
"A View Provider for the Wall object"

View File

@ -44,7 +44,9 @@ typesmap = { "Site": ["IfcSite"],
"Space": ["IfcSpace"],
"Rebar": ["IfcReinforcingBar"],
"Panel": ["IfcPlate"],
"Equipment": ["IfcFurnishingElement","IfcSanitaryTerminal","IfcFlowTerminal","IfcElectricAppliance"]
"Equipment": ["IfcFurnishingElement","IfcSanitaryTerminal","IfcFlowTerminal","IfcElectricAppliance"],
"Pipe": ["IfcPipeSegment"],
"PipeConnector":["IfcPipeFitting"]
}
# which IFC entity (product) is a structural object
@ -55,7 +57,7 @@ structuralifcobjects = (
"IfcStructuralLinearAction", "IfcStructuralLinearActionVarying", "IfcStructuralPlanarAction"
)
# specific name translations
# specific FreeCAD <-> IFC slang translations
translationtable = { "Foundation":"Footing",
"Floor":"BuildingStorey",
"Rebar":"ReinforcingBar",
@ -63,7 +65,9 @@ translationtable = { "Foundation":"Footing",
"ElectricEquipment":"ElectricAppliance",
"Furniture":"FurnishingElement",
"Stair Flight":"StairFlight",
"Curtain Wall":"CurtainWall"
"Curtain Wall":"CurtainWall",
"Pipe Segment":"PipeSegment",
"Pipe Fitting":"PipeFitting"
}
ifctemplate = """ISO-10303-21;
@ -785,7 +789,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
else:
if DEBUG: print "no group name specified for entity: #", ifcfile[host].id(), ", entity type is used!"
grp_name = ifcfile[host].is_a() + "_" + str(ifcfile[host].id())
grp = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup",grp_name)
grp = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup",grp_name.encode("utf8"))
objects[host] = grp
for child in children:
if child in objects.keys():
@ -962,7 +966,7 @@ def export(exportList,filename):
of.write(template.encode("utf8"))
of.close()
os.close(templatefilehandle)
global ifcfile, surfstyles, clones, sharedobjects
global ifcfile, surfstyles, clones, sharedobjects, profiledefs, shapedefs
ifcfile = ifcopenshell.open(templatefile)
history = ifcfile.by_type("IfcOwnerHistory")[0]
context = ifcfile.by_type("IfcGeometricRepresentationContext")[0]
@ -985,6 +989,8 @@ def export(exportList,filename):
sharedobjects = {} # { BaseName: IfcRepresentationMap }
count = 1
groups = {} # { Host: [Child,Child,...] }
profiledefs = {} # { ProfileDefString:profiledef,...}
shapedefs = {} # { ShapeDefString:[shapes],... }
# build clones table
if CREATE_CLONES:
@ -1318,6 +1324,7 @@ def export(exportList,filename):
# 2D objects
if EXPORT_2D:
curvestyles = {}
if annotations and DEBUG: print "exporting 2D objects..."
for anno in annotations:
xvc = ifcfile.createIfcDirection((1.0,0.0,0.0))
@ -1342,6 +1349,22 @@ def export(exportList,filename):
tpl = ifcfile.createIfcAxis2Placement3D(pos,None,None)
txt = ifcfile.createIfcTextLiteral(";".join(anno.LabelText).encode("utf8"),tpl,"LEFT")
reps = [txt]
for coldef in ["LineColor","TextColor","ShapeColor"]:
if hasattr(obj.ViewObject,coldef):
rgb = getattr(obj.ViewObject,coldef)[:3]
if rgb in curvestyles:
psa = curvestyles[rgb]
else:
col = ifcfile.createIfcColourRgb(None,rgb[0],rgb[1],rgb[2])
cvf = ifcfile.createIfcDraughtingPredefinedCurveFont("CONTINUOUS")
ics = ifcfile.createIfcCurveStyle('Line',cvf,None,col)
psa = ifcfile.createIfcPresentationStyleAssignment([ics])
curvestyles[rgb] = psa
for rep in reps:
isi = ifcfile.createIfcStyledItem(rep,[psa],None)
break
shp = ifcfile.createIfcShapeRepresentation(context,'Annotation','Annotation2D',reps)
rep = ifcfile.createIfcProductDefinitionShape(None,None,[shp])
ann = ifcfile.createIfcAnnotation(ifcopenshell.guid.compress(uuid.uuid1().hex),history,anno.Label.encode('utf8'),'',None,gpl,rep)
@ -1390,7 +1413,7 @@ def createCurve(ifcfile,wire):
if da > 0:
follow = not(follow)
xvc = ifcfile.createIfcDirection((1.0,0.0))
ovc = ifcfile.createIfcCartesianPoint(tuple(e.Curve.Center)[:2])
ovc = ifcfile.createIfcCartesianPoint(tuple(e.Curve.Center))
plc = ifcfile.createIfcAxis2Placement2D(ovc,xvc)
cir = ifcfile.createIfcCircle(plc,e.Curve.Radius)
curve = ifcfile.createIfcTrimmedCurve(cir,[ifcfile.createIfcParameterValue(p1)],[ifcfile.createIfcParameterValue(p2)],follow,"PARAMETER")
@ -1404,7 +1427,7 @@ def createCurve(ifcfile,wire):
last = e.Vertexes[-1].Point
else:
last = e.Vertexes[-1].Point
pts = [ifcfile.createIfcCartesianPoint(tuple(v)[:2]) for v in verts]
pts = [ifcfile.createIfcCartesianPoint(tuple(v)) for v in verts]
curve = ifcfile.createIfcPolyline(pts)
segment = ifcfile.createIfcCompositeCurveSegment("CONTINUOUS",True,curve)
segments.append(segment)
@ -1422,6 +1445,7 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
productdef = None
shapetype = "no shape"
tostore = False
subplacement = None
# check for clones
if (not subtraction) and (not forcebrep):
@ -1447,67 +1471,62 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
if (not shapes) and (not forcebrep):
profile = None
if hasattr(obj,"Proxy"):
if hasattr(obj.Proxy,"getProfiles"):
p = obj.Proxy.getProfiles(obj,noplacement=True)
extrusionv = obj.Proxy.getExtrusionVector(obj,noplacement=True)
if not DraftVecUtils.isNull(extrusionv):
extrusionv.multiply(0.001) # to meters
if (len(p) == 1) and extrusionv:
p = p[0].copy()
p.scale(0.001) # to meters
r = obj.Proxy.getPlacement(obj)
r.Base = r.Base.multiply(0.001) # to meters
d = DraftGeomUtils.getNormal(p.Wires[0])
if r.isNull() and ( (p.CenterOfMass.z > 0.001) or ( (d.getAngle(FreeCAD.Vector(0,0,1)) > 0.001) and (d.getAngle(FreeCAD.Vector(0,0,1)) < 3.14159) ) ):
# the object placement is null, but the profile is not in the XY plane.
npla = FreeCAD.Placement()
npla.Base = p.Vertexes[0].Point
nrot = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),d)
npla.Rotation = nrot
r = npla
# p.Placement = p.Placement.multiply(npla.inverse()) # move the profile to origin - not working??
p.translate(p.Vertexes[0].Point.negative())
p.rotate(FreeCAD.Vector(0,0,0),nrot.inverted().Axis,math.degrees(nrot.inverted().Angle))
extrusionv = nrot.inverted().multVec(extrusionv) # move the extrusion vector to Z axis, mandatory in IFC
if hasattr(obj.Proxy,"getExtrusionData"):
extdata = obj.Proxy.getExtrusionData(obj)
if extdata:
# convert to meters
p = extdata[0]
p.scale(0.001)
ev = extdata[1]
ev.multiply(0.001)
pl = extdata[2]
pl.Base = pl.Base.multiply(0.001)
pstr = str([v.Point for v in extdata[0].Vertexes])
if pstr in profiledefs:
profile = profiledefs[pstr]
shapetype = "reusing profile"
else:
if len(p.Edges) == 1:
pxvc = ifcfile.createIfcDirection((1.0,0.0))
povc = ifcfile.createIfcCartesianPoint((0.0,0.0))
pt = ifcfile.createIfcAxis2Placement2D(povc,pxvc)
# extruded circle
if isinstance(p.Edges[0].Curve,Part.Circle):
# extruded circle
profile = ifcfile.createIfcCircleProfileDef("AREA",None,pt, p.Edges[0].Curve.Radius)
# extruded ellipse
elif isinstance(p.Edges[0].Curve,Part.Ellipse):
# extruded ellipse
profile = ifcfile.createIfcEllipseProfileDef("AREA",None,pt, p.Edges[0].Curve.MajorRadius, p.Edges[0].Curve.MinorRadius)
else:
curves = False
for e in p.Edges:
if isinstance(e.Curve,Part.Circle):
curves = True
# extruded polyline
if not curves:
w = Part.Wire(Part.__sortEdges__(p.Edges))
# extruded polyline
w = Part.Wire(Part.__sortEdges__(p.Wires[0].Edges))
pts = [ifcfile.createIfcCartesianPoint(tuple(v.Point)[:2]) for v in w.Vertexes+[w.Vertexes[0]]]
pol = ifcfile.createIfcPolyline(pts)
# extruded composite curve
else:
# extruded composite curve
pol = createCurve(ifcfile,p)
profile = ifcfile.createIfcArbitraryClosedProfileDef("AREA",None,pol)
if profile:
profiledefs[pstr] = profile
if profile and not(DraftVecUtils.isNull(extrusionv)):
xvc = ifcfile.createIfcDirection(tuple(r.Rotation.multVec(FreeCAD.Vector(1,0,0))))
zvc = ifcfile.createIfcDirection(tuple(r.Rotation.multVec(FreeCAD.Vector(0,0,1))))
ovc = ifcfile.createIfcCartesianPoint(tuple(r.Base))
if profile and not(DraftVecUtils.isNull(ev)):
#ev = pl.Rotation.inverted().multVec(ev)
#print "ev:",ev
if not tostore:
# add the object placement to the profile placement. Otherwise it'll be done later at map insert
pl2 = FreeCAD.Placement(obj.Placement)
pl2.Base = pl2.Base.multiply(0.001)
pl = pl2.multiply(pl)
xvc = ifcfile.createIfcDirection(tuple(pl.Rotation.multVec(FreeCAD.Vector(1,0,0))))
zvc = ifcfile.createIfcDirection(tuple(pl.Rotation.multVec(FreeCAD.Vector(0,0,1))))
ovc = ifcfile.createIfcCartesianPoint(tuple(pl.Base))
lpl = ifcfile.createIfcAxis2Placement3D(ovc,zvc,xvc)
edir = ifcfile.createIfcDirection(tuple(FreeCAD.Vector(extrusionv).normalize()))
shape = ifcfile.createIfcExtrudedAreaSolid(profile,lpl,edir,extrusionv.Length)
edir = ifcfile.createIfcDirection(tuple(FreeCAD.Vector(ev).normalize()))
shape = ifcfile.createIfcExtrudedAreaSolid(profile,lpl,edir,ev.Length)
shapes.append(shape)
solidType = "SweptSolid"
shapetype = "extrusion"
@ -1521,33 +1540,34 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
if hasattr(obj.Proxy,"getSubVolume"):
fcshape = obj.Proxy.getSubVolume(obj)
if not fcshape:
if hasattr(obj,"Shape"):
if obj.isDerivedFrom("Part::Feature"):
if False: # below is buggy. No way to duplicate shapes that way?
#if hasattr(obj,"Base") and hasattr(obj,"Additions")and hasattr(obj,"Subtractions"):
if obj.Base and (not obj.Additions) and not(obj.Subtractions):
if obj.Base.isDerivedFrom("Part::Feature"):
if obj.Base.Shape:
if obj.Base.Shape.Solids:
fcshape = obj.Base.Shape
subplacement = FreeCAD.Placement(obj.Placement)
if not fcshape:
if obj.Shape:
if not obj.Shape.isNull():
fcshape = obj.Shape
elif hasattr(obj,"Terrain"):
if obj.Terrain:
if hasattr(obj.Terrain,"Shape"):
if obj.Terrain.Shape:
if not obj.Terrain.Shape.isNull():
fcshape = obj.Terrain.Shape
if fcshape:
solids = []
if fcshape.Solids:
dataset = fcshape.Solids
shapedef = str([v.Point for v in fcshape.Vertexes])
if shapedef in shapedefs:
shapes = shapedefs[shapedef]
shapetype = "reusing brep"
else:
dataset = fcshape.Shells
if DEBUG: print "Warning! object contains no solids"
# if this is a clone, place back the shapes in null position
if tostore:
for shape in dataset:
shape.Placement = FreeCAD.Placement()
# new ifcopenshell serializer
from ifcopenshell import geom
serialized = False
if hasattr(geom,"serialise") and obj.isDerivedFrom("Part::Feature") and SERIALIZE:
p = geom.serialise(obj.Shape.exportBrepToString())
if obj.Shape.Faces:
sh = obj.Shape.copy()
sh.scale(0.001) # to meters
p = geom.serialise(sh.exportBrepToString())
if p:
productdef = ifcfile.add(p)
for rep in productdef.Representations:
rep.ContextOfItems = context
@ -1558,8 +1578,21 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
placement = ifcfile.createIfcLocalPlacement(None,gpl)
shapetype = "advancedbrep"
shapes = None
else:
serialized = True
if not serialized:
# old method
solids = []
if fcshape.Solids:
dataset = fcshape.Solids
else:
dataset = fcshape.Shells
#if DEBUG: print "Warning! object contains no solids"
# if this is a clone, place back the shapes in null position
if tostore:
for shape in dataset:
shape.Placement = FreeCAD.Placement()
for fcsolid in dataset:
fcsolid.scale(0.001) # to meters
faces = []
@ -1577,6 +1610,13 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
if curves:
joinfacets = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch").GetBool("ifcJoinCoplanarFacets",False)
usedae = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch").GetBool("ifcUseDaeOptions",False)
if joinfacets:
result = Arch.removeCurves(fcsolid,dae=usedae)
if result:
fcsolid = result
else:
# fall back to standard triangulation
joinfacets = False
if not joinfacets:
shapetype = "triangulated"
if usedae:
@ -1591,15 +1631,10 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
face = ifcfile.createIfcFace([bound])
faces.append(face)
fcsolid = Part.Shape() # empty shape so below code is not executed
else:
fcsolid = Arch.removeCurves(fcsolid,dae=usedae)
if not fcsolid:
if DEBUG: print "Error: Unable to triangulate shape"
fcsolid = Part.Shape()
for fcface in fcsolid.Faces:
loops = []
verts = [v.Point for v in Part.Wire(Part.__sortEdges__(fcface.OuterWire.Edges)).Vertexes]
verts = [v.Point for v in fcface.OuterWire.OrderedVertexes]
c = fcface.CenterOfMass
v1 = verts[0].sub(c)
v2 = verts[1].sub(c)
@ -1612,7 +1647,7 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
loops.append(bound)
for wire in fcface.Wires:
if wire.hashCode() != fcface.OuterWire.hashCode():
verts = [v.Point for v in Part.Wire(Part.__sortEdges__(wire.Edges)).Vertexes]
verts = [v.Point for v in wire.OrderedVertexes]
v1 = verts[0].sub(c)
v2 = verts[1].sub(c)
if DraftVecUtils.angle(v2,v1,DraftVecUtils.neg(n)) >= 0:
@ -1629,6 +1664,8 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
shape = ifcfile.createIfcFacetedBrep(shell)
shapes.append(shape)
shapedefs[shapedef] = shapes
if shapes:
if tostore:
@ -1638,7 +1675,7 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
ovc = ifcfile.createIfcCartesianPoint((0.0,0.0,0.0))
gpl = ifcfile.createIfcAxis2Placement3D(ovc,zvc,xvc)
repmap = ifcfile.createIfcRepresentationMap(gpl,subrep)
pla = FreeCAD.ActiveDocument.getObject(tostore).Placement
pla = obj.Placement
axis1 = ifcfile.createIfcDirection(tuple(pla.Rotation.multVec(FreeCAD.Vector(1,0,0))))
axis2 = ifcfile.createIfcDirection(tuple(pla.Rotation.multVec(FreeCAD.Vector(0,1,0))))
origin = ifcfile.createIfcCartesianPoint(tuple(FreeCAD.Vector(pla.Base).multiply(0.001)))