示例#1
0
def make_compound(listOfShapes):
    aRes = TopoDS.TopoDS_Compound()
    aBuilder = BRep.BRep_Builder()
    aBuilder.MakeCompound(aRes)
    for item in listOfShapes:
        aBuilder.Add(aRes, item)
    aBuilder._InputShapes = listOfShapes
    aRes._builder = aBuilder
    return aRes    
示例#2
0
 def compound(self):
     """ Create and returns a compound from the _shapes list"""
     # Create a compound
     compound = TopoDS.TopoDS_Compound()
     brep_builder = BRep.BRep_Builder()
     brep_builder.MakeCompound(compound)
     # Populate the compound
     for shape in self._shapes:
         brep_builder.Add(compound, shape)
     return compound
示例#3
0
def fixShape(shape):
    """
		fixes a shape
	"""
    log.info("Fixing holes and degenerated Meshes...")
    sf = ShapeFix.ShapeFix_Shape(shape)
    sf.SetMaxTolerance(TOLERANCE)

    msgRegistrator = ShapeExtend.ShapeExtend_MsgRegistrator()
    sf.SetMsgRegistrator(msgRegistrator.GetHandle())

    sf.Perform()

    log.info("ShapeFix Complete.")
    for i in range(0, 18):
        log.info("ShapeFix Status %d --> %s" % (i, sf.Status(i)))

    fixedShape = sf.Shape()
    #fixedShape = shape;
    return fixedShape
    #if the resulting shape is a compound, we need to convert
    #each shell to a solid, and then re-create a new compound of solids
    if fixedShape.ShapeType() == TopAbs.TopAbs_COMPOUND:

        log.warn("Shape is a compound. Creating solids for each shell.")
        builder = BRep.BRep_Builder()
        newCompound = TopoDS.TopoDS_Compound()
        #newCompound = TopoDS.TopoDS_CompSolid();
        builder.MakeCompound(newCompound)
        #builder.MakeCompSolid(newCompound);
        for shell in Topo(fixedShape).shells():

            solidBuilder = BRepBuilderAPI.BRepBuilderAPI_MakeSolid(shell)
            solid = solidBuilder.Solid()
            sa = SolidAnalyzer(solid)
            print sa.friendlyDimensions()
            builder.Add(newCompound, solid)

        #time.sleep(4);
        #Topology.dumpTopology(newCompound);
        return newCompound
        #return temporarily after the first one
    else:
        log.info("Making Solid from the Shell...")

        solidBuilder = BRepBuilderAPI.BRepBuilderAPI_MakeSolid(
            ts.Shell(fixedShape))
        return solidBuilder.Solid()
示例#4
0
def drillWithHolesFaster(shape):
    "returns a shape with a bunch of spheres removed from it"
    box = Bnd.Bnd_Box()
    b = BRepBndLib.BRepBndLib()
    b.Add(shape, box)

    (xMin, yMin, zMin, xMax, yMax, zMax) = box.Get()

    d = 0.05 * ((xMax - xMin))
    di = 3.0 * d
    vec = gp.gp_Vec(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(0, 0, d))
    cp = None
    compound = TopoDS.TopoDS_Compound()
    builder = BRep.BRep_Builder()
    builder.MakeCompound(compound)
    #drill holes in a rectangular grid
    for x in frange6(xMin, xMax, di):
        for y in frange6(yMin, yMax, di):
            for z in frange6(zMin, zMax, di):
                #make a sphere
                center = gp.gp_Pnt(x, y, z)
                hole = BRepPrimAPI.BRepPrimAPI_MakeSphere(center, d).Shape()
                #lets see if a square hole is faster!
                #w = squareWire(center,d );
                #hb = BRepPrimAPI.BRepPrimAPI_MakePrism(w,vec,False,True);
                #hb.Build();
                #hole = hb.Shape();
                builder.Add(compound, hole)

    display.DisplayShape(compound)
    q = time.clock()
    cut = BRepAlgoAPI.BRepAlgoAPI_Cut(shape, compound)
    if cut.ErrorStatus() == 0:
        print "Cut Took %0.3f sec." % (time.clock() - q)
        return cut.Shape()
    else:
        print "Error Cutting: %d" % cut.ErrorStatus()
        return shape
示例#5
0
    def compound(self):
        """ Create and returns a compound from the _shapes list

        Returns
        -------
        TopoDS.TopoDS_Compound

        Notes
        -----
        Importing an iges box results in:
        0 solid
        0 shell
        6 faces
        24 edges

        """
        # Create a compound
        compound = TopoDS.TopoDS_Compound()
        brep_builder = BRep.BRep_Builder()
        brep_builder.MakeCompound(compound)
        # Populate the compound
        for shape in self._shapes:
            brep_builder.Add(compound, shape)
        return compound
示例#6
0
def copy(shape):
    """Return a copy of the shape that does not reference the original shape"""
    new = BRepAlgoAPI.BRepAlgoAPI_Common(shape, cube).Shape()
    comp = TopoDS.TopoDS_Compound(new)
    return subshapes(comp, shape.ShapeType())[0]
示例#7
0
    def hatch(self):
        """take the a slice and compute hatches
		
			computing the intersections of wires is very expensive, 
			so it is important to do this as efficently as possible.
			
			Inputs: a set of boundary wires that define a face,
			        a set of filling wires that cover a bounding box around the face.
			
			Outputs: 
				trim covering wires/edges by the boundaries, 
				trim boundaries by intersections with wires
				
			optimizations so far:
				* use active wire table to detect when to stop computing intersections with wires.
				 each boundary must be activated before computation stops, and a boundary is finished
				 after intersections have been found, and then stop occurring.
				 
			More optimization can be used by Bnd_BoundSortBox-- which would compare all of the bounding
			boxes at once in c++, but with more glue code.
			
		"""
        q = time.clock()
        hatchWires = self._makeHatchLines()

        numCompares = 0
        bbuilder = BRep.BRep_Builder()
        comp = TopoDS.TopoDS_Compound()
        bbuilder.MakeCompound(comp)

        #print "Time to Make hatch Lines: %0.3f" % ( time.clock() - q )

        for b in self.boundaryWires:
            bbuilder.Add(comp, b)
            self.graphBuilder.addBoundaryWire(b)

        #print "There are %d boundary wires, %d hatch wires" % ( len(self.boundaryWires),len(hatchWires)  )
        #intersect each line with each boundary

        brp = BRepExtrema.BRepExtrema_DistShapeShape()
        brp.LoadS1(comp)

        for hatchLine in hatchWires:

            interSections = []
            #list of intersections for just this single hatch line
            closePoints = []
            #extrema that are not intersectionsf or this single hatch line

            brp.LoadS2(hatchLine)
            numCompares += 1
            result = brp.Perform()
            """
				tricky thing: for a given hatch line ( whether a line or a string of hexes ),
				we want to check for both intersections and close points that would result in overdrawing.
				if the line has zero intersections, then it is not in play, and should not be considered.
				if the line has at least one intersection, then it is in play, and extrema that are not intersections should
				also be included for later processing.
			"""
            if result and brp.Value(
            ) < 0.050:  #if < tolerance we have an intersection. if < filament width we have a close sitation
                #print "intersection found, distance = %0.6f" % brp.Value()
                #TODO need to handle the somewhat unusual cases that the intersection is
                #on a vertex
                for k in range(1, brp.NbSolution() + 1):
                    if brp.Value(
                    ) < 0.001:  #there is at least one intersection on this wire. there may also be extrema
                        #print "spot on match"
                        #try:
                        #make the node
                        #quite complex depending on where exactly the intersection is.
                        if brp.SupportTypeShape1(
                                k) == BRepExtrema.BRepExtrema_IsOnEdge:
                            #well this sux-- have to check to ensure that the edge is not a local
                            #minimum or maximum also.
                            if OCCUtil.isEdgeLocalMinimum(
                                    OCCUtil.cast(brp.SupportOnShape1(k)),
                                    brp.ParOnEdgeS1(k), brp.PointOnShape1(k)):
                                print "Warning: edge appears to be a local min/max.  Is it a tangent?"
                                continue
                            else:
                                #self.boundaryIntersectionsByEdge = {} #boundary intersections, hashed by edges
                                self.graphBuilder.addPointOnBoundaryEdge(
                                    OCCUtil.cast(brp.SupportOnShape1(k)),
                                    brp.ParOnEdgeS1(k), brp.PointOnShape1(k))

                        if brp.SupportTypeShape2(
                                k) == BRepExtrema.BRepExtrema_IsOnEdge:
                            if brp.SupportTypeShape1(
                                    k) == BRepExtrema.BRepExtrema_IsVertex:
                                #the intersection is on a vertex of the boundary.
                                vertex = OCCUtil.cast(brp.SupportOnShape1(k))

                            #otherwise, vertex was not a local minimum, or was on an edge
                            poe = eg.PointOnAnEdge(
                                OCCUtil.cast(brp.SupportOnShape2(k)),
                                brp.ParOnEdgeS2(k), brp.PointOnShape2(k))
                            interSections.append(poe)

                        elif brp.SupportTypeShape2(
                                k) == BRepExtrema.BRepExtrema_IsVertex:
                            #how on earth to handle this one?
                            #this actually means that a vertex can also have an infill node attached to it!
                            #for right now let's just ignore it.
                            print "WARNING: intersection on vertex of hatch line!"
                            pass
                        else:
                            raise ValueError(
                                "I dont know how to handle this kind of intersection"
                            )
                    else:  #brp.Value is between 0.001 and 0.05
                        #print "intersection is close";
                        #we know this is a place where the boundary is close to a fill contour.
                        #our goal is to eventually remove it from the list. Support1 is the boundary.
                        #print "found extremum close but not intersecting, distance = %0.3f" %  ( brp.Value() )
                        if brp.SupportTypeShape1(
                                k) == BRepExtrema.BRepExtrema_IsOnEdge:
                            poeBound = eg.PointOnAnEdge(
                                brp.SupportOnShape1(k), brp.ParOnEdgeS1(k),
                                brp.PointOnShape1(k))
                            closePoints.append(
                                (poeBound, 'EDGE', brp.SupportOnShape2(k)))
                        elif brp.SupportTypeShape2(
                                k) == BRepExtrema.BRepExtrema_IsVertex:
                            #here a vertex is closest to a fill line.
                            poeBound = eg.PointOnAnEdge(
                                brp.SupportOnShape1(k), 0,
                                brp.PointOnShape1(k))
                            closePoints.append(
                                (poeBound, 'VERTEX', brp.SupportOnShape2(k)))

            if len(interSections) % 2 == 1:
                print "Detected Odd Number of intersection points. This is ignored for now."
                continue

            if len(interSections) == 0:
                #print "Hatch has no intersections-- discarding";
                continue

            #at this point we have all the intersections for this hatch line.
            #we also know we have at least one intersection.

            if len(closePoints) > 0:
                #there were extrema ( points where this hatch is close to a boundary but doesnt intersect.
                #we do this here instead of inline because we have to avoid hatch lines that are close, but do not actually
                #intersect a wire. ( ie, they are 'just outside' of the boundary )
                for cp in closePoints:
                    self.graphBuilder.addPointsTooClose(cp[0], cp[1])
                    #display.DisplayShape(cp[0].edge );
                    #display.DisplayShape(cp[2] );

            #add the edges 'inside' the shape to the graph
            #print "Splitting wire by %d intersection points" % len(interSections )
            edgesInside = []
            edgesInside = eg.splitWire(hatchLine, interSections)

            #returned value is a list of lists. each entry is a chain of edges
            for e in edgesInside:
                self.graphBuilder.addFillEdges(e)

            #test to see if we can break out of the loop.
            #we can stop if we've hit each boundary at least once
            #if len(interSections) == 0 and (len(boundariesFound) ==  len(self.boundaryWires)):
            #	continueHatching = False;

        print "%d Total Intersections computed." % (numCompares)
        self.graphBuilder.buildGraph()