def createShape3d(doc, pts, indices, shape, key, prc, mat): name = shape.getFirst(TYP_NAME).data cnt = len(pts) if (cnt > 0): if (key is not None): name = "%s_%d" % (name, key) mtx = createMatrix(prc) # translate the points according to the transformation matrix pt = numpy.ones((cnt, 4), numpy.float32) pt[:, :3] = pts tpt = numpy.transpose(numpy.dot(mtx, numpy.transpose(pt))) data = [] for pol in indices: if (len(pol) > 2): # skip points and lines! try: ngon = [tpt[idx][0:3] for idx in pol] for triangle in triangulate.getTriangles(ngon): data.append(triangle) except: pass if (len(data) > 0): obj = newObject(doc, name, data) adjustMaterial(obj, mat) return True FreeCAD.Console.PrintWarning("no faces ... ") return True
def createObject(doc, dmsh): FreeCAD.Console.PrintMessage("Adding '%s' " % (dmsh.name)) xfrm = dmsh.parent mtx = xfrm.getMatrix() msh = dmsh.getPropertyId('MESH') if (msh is not None): vt = msh.data[1] indices = getIndices(msh) cnt = len(vt) # translate the points according to the transformation matrix pt = numpy.ones((cnt, 4), numpy.float32) pt[:, :3] = vt tpt = numpy.transpose(numpy.dot(mtx, numpy.transpose(pt))) if (len(indices) > 0): data = [] for pol in indices: if (len(pol) > 2): # skip points and lines! try: ngon = [tpt[idx][0:3] for idx in pol] for triangle in triangulate.getTriangles(ngon): data.append(triangle) except: pass if (len(data) > 0): obj = newObject(doc, dmsh.name, data) else: FreeCAD.Console.PrintWarning("... no faces ... ") else: FreeCAD.Console.PrintWarning( "... failed - Unknown mesh format at %X " % (dmsh.pos)) FreeCAD.Console.PrintMessage("Done!\n")
def createShape(self, chopper, face, mtx, name, pts): # translate the points according to the transformation matrix pt = numpy.ones((len(pts), 4), numpy.float32) pt[:, :3] = pts tpt = numpy.transpose(numpy.dot(mtx, numpy.transpose(pt))) data = [] for idx in self.data: data.append([tpt[idx[0]], tpt[idx[1]], tpt[idx[2]]]) obj = newObject(chopper.tg, name, data) chopper.adjustMaterial(obj, face) return
def buildObject(doc, parent, layer, objMaterials, objTags): FreeCAD.Console.PrintMessage("Building mesh '%s'...\n" % (layer.name)) data = [] mfacets = [] if (layer.pols): for pol in layer.pols: ngon = [layer.pnts[idx] for idx in pol] points = [V(k) for k in ngon] points.append(points[0]) if len(ngon) > 2: wire = Part.makePolygon(points) plane = wire.findPlane(0.00001) points2 = [plane.projectPoint(p) for p in points] wire = Part.makePolygon(points2) try: face = Part.Face(wire) tris = face.tessellate(0.1) for tri in tris[1]: data.append([tris[0][i] for i in tri]) except Exception as e: Part.show(wire) FreeCAD.Console.PrintWarning( " skipping polygon (%s): %s\n" % (e, ngon)) me = newObject(doc, layer.name, data) me.Placement.Base = FreeCAD.Vector(layer.pivot) # Create the Material Slots and assign the MatIndex to the correct faces. for surf_key in layer.surf_tags: if objTags[surf_key] in objMaterials: material = objMaterials[objTags[surf_key]] # me.ViewObject.ShapeMaterial.AmbientColor = me.ViewObject.ShapeMaterial.DiffuseColor = material.colr # me.ViewObject.ShapeMaterial.EmissiveColor = # me.ViewObject.ShapeMaterial.SpecularColor = me.ViewObject.ShapeMaterial.Shininess = material.lumi me.ViewObject.ShapeMaterial.Transparency = material.trnl # Clear out the dictionaries for this layer. layer.surf_tags.clear() if (len(layer.subds) == 0): if (parent is not None): parent.Shapes.append(me) else: group = newGroup(doc, layer.name) group.Shapes.append(me) if (parent is not None): parent.Shapes.append(group) for child in layer.subds: buildObject(doc, group, child, objMaterials, objTags)
def readBody(self, st, doc): self.resetMaterial() number = getInteger(st) if (len(self.vertexList) > 0): data = [] faces = self.getFaces() for f in faces: p0 = self.vertexList[f[0]] p1 = self.vertexList[f[1]] p2 = self.vertexList[f[2]] data.append([p0, p1, p2]) mesh = newObject(doc, self.currentName, data) self.adjustMaterial(mesh)
def initialize(self, chopper): hi = self.getSubChunk(HIERARCHY_INFO) # build up tree! hrx = self.getSubChunk(HIERARCHY) FreeCAD.Console.PrintMessage("Adding '%s'\n" % (hrx.name)) #mtx = self.createKeyMatrix(chopper.currentFrame) mtx = self.createKeyMatrix(0) parent = chopper.keyMatrix.get(hrx.getParentId()) if (parent is not None): prnMtx = parent.matrix if (prnMtx is not None): mtx = mtx.dot(prnMtx) self.matrix = mtx chopper.keyMatrix[hi.data] = self nObj = chopper.namedObjectes.get(hrx.name) if (nObj): mObj = nObj.getSubChunk(TRI_MESH_OBJ) if (mObj): pts = getData( mObj, TRI_VERTEXL ) # the 3D-point coordinates => Vertex3ListChunk dsc = mObj.getSubChunk( FACES_DESCRIPTION ) # the 3D object faces => FacesDescriptionChunk if (dsc): mat = dsc.getSubChunk(TRI_MATERIAL) plc = mObj.getSubChunk( TRI_PLACEMENT ) # the 3D object faces => PlacementChunk mtx = numpy.dot(plc.getMatrix(), mtx) # translate the points according to the transformation matrix pt = numpy.ones((len(pts), 4), numpy.float32) pt[:, :3] = pts tpt = numpy.transpose(numpy.dot(mtx, numpy.transpose(pt))) data = [] for idx in dsc.data: data.append([tpt[idx[0]], tpt[idx[1]], tpt[idx[2]]]) obj = newObject(chopper.tg, hrx.name, data) chopper.adjustMaterial(obj, mat)
def buildObject(doc, parent, layer, objMaterials, objTags): FreeCAD.Console.PrintMessage("Building mesh '%s'...\n" %(layer.name)) data = [] for pol in layer.pols: if len(pol) > 2:# skip points and lines! ngon = [layer.pnts[idx] for idx in pol] try: for triangle in triangulate.getTriangles(ngon): data.append(triangle) except: FreeCAD.Console.PrintWarning(" skipping polygon that is too small:%s\n"%(ngon)) me = newObject(doc, layer.name, data) me.Placement.Base = FreeCAD.Vector(layer.pivot) # Create the Material Slots and assign the MatIndex to the correct faces. for surf_key in layer.surf_tags: if objTags[surf_key] in objMaterials: material = objMaterials[objTags[surf_key]] # me.ViewObject.ShapeMaterial.AmbientColor = me.ViewObject.ShapeMaterial.DiffuseColor = material.colr[:] # me.ViewObject.ShapeMaterial.EmissiveColor = # me.ViewObject.ShapeMaterial.SpecularColor = me.ViewObject.ShapeMaterial.Shininess = material.lumi me.ViewObject.ShapeMaterial.Transparency = material.trnl # Clear out the dictionaries for this layer. layer.surf_tags.clear() if (len(layer.subds) == 0): if (parent is not None): parent.Shapes.append(me) else: group = newGroup(doc, layer.name) group.Shapes.append(me) if (parent is not None): parent.Shapes.append(group) for child in layer.subds: buildObject(doc, group, child, objMaterials, objTags)