Esempio n. 1
0
def getPntsCurveSurfaceIntersect(curve, surface):
    pnts = []
    tool = GeomAPI_IntCS(curve, surface)
    pCount = tool.NbPoints();
    for i in range(pCount):
       pnts += [tool.Point(1)]
    return pnts
Esempio n. 2
0
 def findIntersectPoints(curve, surface):
     ps = []
     tool = GeomAPI_IntCS(curve, surface)
     pCount = tool.NbPoints()
     for i in range(1, pCount + 1):
         ps += [tool.Point(i)]
     return ps
Esempio n. 3
0
    def intersections_with_line(self, line):
        """Compute the intersections with a line.

        Parameters
        ----------
        line : :class:`~compas.geometry.Line`

        Returns
        -------
        list[:class:`~compas.geometry.Point`]

        """
        intersection = GeomAPI_IntCS(Geom_Line(line.to_occ()),
                                     self.occ_surface)
        points = []
        for index in range(intersection.NbPoints()):
            pnt = intersection.Point(index + 1)
            point = Point.from_occ(pnt)
            points.append(point)
        return points
Esempio n. 4
0
class Sketch(object):
    def __init__(self, theDisplay, sg: Sketch_QTGUI = None):
        self.myCoordinateSystem = gp_Ax3(gp.XOY())
        self.myContext: AIS_InteractiveContext = theDisplay.Context
        self.myDisplay = theDisplay
        self.myView: V3d_View = theDisplay.View
        self.myGUI = sg
        self.myGUI.SetAx3(self.myCoordinateSystem)
        self.myGUI.SetContext(self.myContext)

        self.myCurrentMethod = Sketch_ObjectTypeOfMethod.Nothing_Method

        self.myCurrentDir: gp_Dir = gp.DZ()
        self.myTempPnt: gp_Pnt = gp.Origin()
        self.myCurrentPnt2d: gp_Pnt2d = gp.Origin2d()
        self.myCurrentPlane = Geom_Plane(self.myCoordinateSystem)
        self.myCurrentLine = Geom_Line(self.myTempPnt, self.myCurrentDir)

        self.myIntCS = GeomAPI_IntCS()

        self.PolylineFirstPoint = gp.Origin2d()
        self.PolylineFirstPointExist = False

        self.myData = []
        self.myNode: SketchObjectNode = None
        self.myModel = None
        self.SnapToGridPoint = False
        self.myCommands = []

        self.myAnalyserSnap = Sketch_AnalyserSnap(self.myContext, self.myData,
                                                  self.myCoordinateSystem)

        self.addCommand(Sketch_CommandPoint())
        self.addCommand(Sketch_CommandLine2P())
        self.addCommand(Sketch_CommandBezierCurve())
        self.addCommand(Sketch_CommandArcCenter2P())
        self.addCommand(Sketch_CommandCircleCenterRadius())
        self.addCommand(Sketch_CommandBSpline())
        self.addCommand(Sketch_CommandPointToBSpline())
        self.addCommand(Sketch_CommandMoveObject())
        self.addCommand(Sketch_CommandNurbCircleSquare())
        self.addCommand(Sketch_CommandNurbCircleTriangle())

    def SetContext(self, theContext: AIS_InteractiveContext):
        self.myContext = theContext
        self.myAnalyserSnap.SetContext(self.myContext)
        self.myGUI.SetContext(theContext)
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetContext(self.myContext)

    def SetData(self, thedata: list):
        self.myData = thedata
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetData(self.myData)

    def SetRootNode(self, theNode):
        self.myNode = theNode
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetRootNode(self.myNode)

    def SetModel(self, theModel):
        self.myModel = theModel
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetModel(self.myModel)

    def GetData(self):
        return self.myData

    def SetCoordinateSystem(self, theCS: gp_Ax3):
        self.myCoordinateSystem = theCS
        self.myCurrentPlane.SetPosition(self.myCoordinateSystem)
        self.myAnalyserSnap.SetAx3(self.myCoordinateSystem)
        self.myGUI.SetAx3(self.myCoordinateSystem)
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetAx3(self.myCoordinateSystem)

    def GetCoordinateSystem(self):
        return self.myCoordinateSystem

    def SetPrecise(self, aPrecise: float):
        if aPrecise > 0:
            self.myAnalyserSnap.SetMinDistance(aPrecise)

    def SetColor(self, theColor: Quantity_Color):
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetColor(theColor)

    def SetType(self, theType: Sketch_ObjectType):
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetType(theType)

    def SetStyle(self, theLineStyle: Prs3d_LineAspect):
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            self.CurCommand.SetStyle(theLineStyle)

    def ObjectAction(self, theMethod: Sketch_ObjectTypeOfMethod):
        self.myCurrentMethod = theMethod
        self.SelectCurCommand()
        self.CurCommand.Action()
        if (self.myCurrentMethod == Sketch_ObjectTypeOfMethod.Line2P_Method
                or self.myCurrentMethod
                == Sketch_ObjectTypeOfMethod.ArcCenter2P_Method
            ) and self.PolylineFirstPointExist:
            self.CurCommand.SetPolylineFirstPnt(self.PolylineFirstPoint)
        else:
            self.PolylineFirstPointExist = False

    def GetStatus(self):
        return self.myCurrentMethod

    def ProjectPointOnPlane(self, v3dX, v3dY, v3dZ, projVx, projVy, projVz):
        self.myTempPnt.SetCoord(v3dX, v3dY, v3dZ)
        self.myCurrentDir.SetCoord(projVx, projVy, projVz)

        self.myCurrentLine.SetDirection(self.myCurrentDir)
        self.myCurrentLine.SetLocation(self.myTempPnt)
        # pnt=self.myCurrentPlane.Pln().Location ()
        # print(pnt.X(),pnt.Y(),pnt.Z())
        self.myIntCS.Perform(
            self.myCurrentLine,
            self.myCurrentPlane)  # perfrom intersection calculation
        if self.myIntCS.NbPoints() >= 1:
            self.myTempPnt = self.myIntCS.Point(1)
            self.myCurrentPnt2d.SetX(
                (self.myTempPnt.X() - self.myCoordinateSystem.Location().X()) *
                self.myCoordinateSystem.XDirection().X() +
                (self.myTempPnt.Y() - self.myCoordinateSystem.Location().Y()) *
                self.myCoordinateSystem.XDirection().Y() +
                (self.myTempPnt.Z() - self.myCoordinateSystem.Location().Z()) *
                self.myCoordinateSystem.XDirection().Z())
            self.myCurrentPnt2d.SetY(
                (self.myTempPnt.X() - self.myCoordinateSystem.Location().X()) *
                self.myCoordinateSystem.YDirection().X() +
                (self.myTempPnt.Y() - self.myCoordinateSystem.Location().Y()) *
                self.myCoordinateSystem.YDirection().Y() +
                (self.myTempPnt.Z() - self.myCoordinateSystem.Location().Z()) *
                self.myCoordinateSystem.YDirection().Z())
            return True
        else:
            return False

    def OnMouseInputEvent(self, *kargs):
        theX, theY, buttons, modifier = kargs
        aView: V3d_View = self.myView
        v3dX, v3dY, v3dZ, projVx, projVy, projVz = aView.ConvertWithProj(
            theX, theY)
        if self.SnapToGridPoint:
            # grid echo
            v3dX, v3dY, v3dZ = self.myView.ConvertToGrid(theX, theY)
            self.myDisplay.Viewer.ShowGridEcho(
                self.myView, Graphic3d_Vertex(v3dX, v3dY, v3dZ))
            self.myDisplay.Viewer.SetGridEcho(True)
        if self.ProjectPointOnPlane(v3dX, v3dY, v3dZ, projVx, projVy, projVz):
            self.SelectCurCommand()
            if self.CurCommand.MouseInputEvent(self.myCurrentPnt2d, buttons,
                                               modifier):
                self.myCurrentMethod = Sketch_ObjectTypeOfMethod.Nothing_Method

    def OnMouseMoveEvent(self, *kargs):
        theX, theY, buttons, modifier = kargs
        aView: V3d_View = self.myView
        v3dX, v3dY, v3dZ, projVx, projVy, projVz = aView.ConvertWithProj(
            theX, theY)
        if self.SnapToGridPoint:
            # grid echo
            v3dX, v3dY, v3dZ = self.myView.ConvertToGrid(theX, theY)
            self.myDisplay.Viewer.ShowGridEcho(
                self.myView, Graphic3d_Vertex(v3dX, v3dY, v3dZ))
            self.myDisplay.Viewer.SetGridEcho(True)
        if self.ProjectPointOnPlane(v3dX, v3dY, v3dZ, projVx, projVy, projVz):
            self.SelectCurCommand()
            self.CurCommand.MouseMoveEvent(self.myCurrentPnt2d, buttons,
                                           modifier)

    def OnMouseReleaseEvent(self, *kargs):
        buttons, modifier = kargs
        self.SelectCurCommand()
        self.CurCommand.MouseReleaseEvent(buttons, modifier)

    def GetCurPoint2D(self):
        return self.myCurrentPnt2d

    def GetCurPoint3D(self):
        return elclib.To3d(self.myCoordinateSystem.Ax2(), self.myCurrentPnt2d)

    def SetPickTolerance(self, thePrecision):
        # selector
        selector_manager = self.myContext.MainSelector()
        # self._display.Context.SetPixelTolerance(5)
        selector_manager.SetPixelTolerance(thePrecision)
        print(selector_manager.PixelTolerance())

    def OnCancel(self):
        self.SelectCurCommand()
        self.myAnalyserSnap.Cancel()
        if (self.myCurrentMethod == Sketch_ObjectTypeOfMethod.Line2P_Method
                and self.myCurrentMethod
                == Sketch_ObjectTypeOfMethod.ArcCenter2P_Method):
            self.PolylineFirstPointExist = self.CurCommand.GetPolylineFirstPnt(
                self.PolylineFirstPoint)
        self.CurCommand.CancelEvent()
        self.myCurrentMethod = Sketch_ObjectTypeOfMethod.Nothing_Method
        # Acitivate selection automaticlly
        # self.myContext.SetAutoActivateSelection(True)
        # self.SetPickTolerance(20)
        # self.myContext.ActivatedModes()
        # # for all the sketch object selectable
        # self.myContext.Deactivate()
        self.myContext.Activate(0)

    def DeleteSelectedObject(self):
        index = 0
        while index < self.myNode.childCount():
            child = self.myNode.child(index)
            assert isinstance(child, SketchObjectNode)
            myCurObject: Sketch_Geometry = child.getSketchObject()
            if self.myContext.IsSelected(myCurObject.GetAIS_Object()):
                myCurObject.RemoveDisplay()
                self.myNode.removeChild(index)
            else:
                index += 1

    def ViewProperties(self):
        # for idx in range(len(self.myData)):
        #     myCurObject: Sketch_Object = self.myData[idx]
        #     if self.myContext.IsSelected(myCurObject.GetAIS_Object()):
        #         self.myContext.ClearSelected(True)
        #         self.myGUI.SetSketch_Object(myCurObject)
        #         break
        for child in self.myNode.children():
            assert isinstance(child, SketchObjectNode)
            myCurObject: Sketch_Geometry = child.getSketchObject()
            if self.myContext.IsSelected(myCurObject.GetAIS_Object()):
                self.myContext.ClearSelected(True)
                self.myGUI.SetSketch_Object(myCurObject)
                break

    def RedrawAll(self):
        for idx in range(len(self.myData)):
            myCurObject: Sketch_Object = self.myData[idx]
            if self.myContext.IsSelected(myCurObject.GetAIS_Object()):
                self.myContext.Display(myCurObject.GetAIS_Object(), True)

    def SetPolylineMode(self, amode):
        for idx in range(len(self.myCommands)):
            self.CurCommand = self.myCommands[idx]
            self.CurCommand.SetPolylineMode(amode)

    def SetSnap(self, theSnap):
        self.myAnalyserSnap.SetSnapType(theSnap)

    def GetSnap(self):
        return self.myAnalyserSnap.GetSnapType()

    def addCommand(self, theCommand: Sketch_Command):
        theCommand.SetData(self.myData)
        theCommand.SetContext(self.myContext)
        theCommand.SetRootNode(self.myNode)
        theCommand.SetAnalyserSnap(self.myAnalyserSnap)

        theCommand.SetAx3(self.myCoordinateSystem)
        self.myCommands.append(theCommand)

    def SelectCurCommand(self):
        for idx in range(len(self.myCommands)):
            self.CurCommand: Sketch_Command = self.myCommands[idx]
            if self.CurCommand.GetTypeOfMethod() == self.myCurrentMethod:
                # Acitivate selection automaticlly
                # This is a bug!! Remove the following line will lead to critical error when enable sketch command.
                self.myContext.SetAutoActivateSelection(False)
                break
Esempio n. 5
0
points2.append(Point(-2, 2, 0))
points2.append(Point(2, 3, -1))
points2.append(Point(3, 7, -2))
points2.append(Point(4, 9, -1))
spline2 = BSplineCurve.from_points(points2)

surface = BSplineSurface.from_fill(spline1, spline2)
line = Geom_Line(gp_Pnt(0, 4, 0), gp_Dir(0, 0, 1))

# ==============================================================================
# Intersection
# ==============================================================================

intersection = GeomAPI_IntCS(line, surface.occ_surface)

pnt = intersection.Point(1)
point = Point.from_occ(pnt)

# ==============================================================================
# Viz
# ==============================================================================

mesh = surface.to_vizmesh()
boundary = Polyline(
    mesh.vertices_attributes('xyz', keys=mesh.vertices_on_boundary()))

view = App()
view.add(mesh)
view.add(boundary, linewidth=2)
view.add(point, size=10, color=(1, 0, 0))
view.run()
Esempio n. 6
0
class HexPlane(plotocc):
    def __init__(self):
        plotocc.__init__(self)
        self.compound = TopoDS_Compound()
        self.builder = BRep_Builder()
        self.builder.MakeCompound(self.compound)

        self.beam = gp_Ax3()
        self.beam.SetLocation(gp_Pnt(0.5, 0.5, 0.0))
        self.beam.SetDirection(gp_Dir(0.0, 0.5, 1.0))
        self.beam_line = line_from_axs(self.beam, length=20)
        self.builder.Add(self.compound, self.beam_line)

        ax = gp_Ax3(gp_Pnt(0, 0, 10), gp_Dir(0, 0, -1))
        px = np.linspace(-1, 1, 10) * 10
        py = np.linspace(-1, 1, 10) * 10
        mesh = np.meshgrid(px, py)
        surf = mesh[0]**2 / 100 + mesh[1]**2 / 150
        self.surf = spl_face(*mesh, surf, ax)
        self.surf_bound = self.make_PolySurf(radi=5, axs=ax)

        self.beam_glin = Geom_Line(self.beam.Location(), self.beam.Direction())
        self.ics = GeomAPI_IntCS(self.beam_glin, BRep_Tool.Surface(self.surf))
        print(self.ics.NbPoints())
        # print(self.ics.Point(1))

        self.ics = GeomAPI_IntCS(self.beam_glin,
                                 BRep_Tool.Surface(self.surf_bound))
        print(self.ics.NbPoints())

        #self.display.DisplayShape(self.surf, transparency=0.7)
        self.display.DisplayShape(self.surf_bound, transparency=0.7)
        self.plns = TopoDS_Shell()
        self.builder.MakeShell(self.plns)
        for ix in np.linspace(0, 1, 5):
            for iy in np.linspace(0, 1, 5):
                p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec()
                GeomLProp_SurfaceTool.D1(BRep_Tool.Surface(self.surf), ix, iy,
                                         p1, vx, vy)
                vz = vx.Crossed(vy)
                axs = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx))
                pln = self.make_PolyPlane(axs=axs, radi=2.5, shft=15.0)
                print(pln)

                self.builder.Add(self.compound, make_vertex(p1))
                self.builder.Add(self.plns, pln)
        self.builder.Add(self.compound, self.plns)

        for face in Topo(self.plns).faces():
            self.ics.Perform(self.beam_glin, BRep_Tool.Surface(face))
            uvw = self.ics.Parameters(1)
            u, v, w = uvw
            p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec()
            GeomLProp_SurfaceTool.D1(BRep_Tool.Surface(face), u, v, p1, vx, vy)
            vz = vx.Crossed(vy)
            if u > 0 and v > 0:
                print(u, v)
                print(p1)
                print(self.ics.Point(1))
                self.display.DisplayShape(p1)
                self.display.DisplayShape(face, color="BLUE")
            else:
                print(u, v)

    def make_PolyPlane(self, num=6, radi=1.0, shft=0.0, axs=gp_Ax3()):
        lxy = radi - 0.1
        pnts = []
        angl = 360 / num
        for i in range(num):
            thet = np.deg2rad(i * angl) + np.deg2rad(shft)
            x, y = radi * np.sin(thet), radi * np.cos(thet)
            pnts.append(gp_Pnt(x, y, 0))
        pnts.append(pnts[0])
        poly = make_polygon(pnts)
        brep = BRepBuilderAPI_MakeFace(gp_Pln(), poly)
        brep.Add(poly)
        face = brep.Face()
        face.Location(set_loc(gp_Ax3(), axs))
        return face

    def make_PolySurf(self, num=6, radi=1.0, shft=0.0, axs=gp_Ax3()):
        lxy = radi - 0.1
        pnts = []
        angl = 360 / num
        for i in range(num):
            thet = np.deg2rad(i * angl) + np.deg2rad(shft)
            x, y = radi * np.sin(thet), radi * np.cos(thet)
            pnts.append(gp_Pnt(x, y, 0))
        pnts.append(pnts[0])
        poly = make_polygon(pnts)
        proj = BRepProj_Projection(poly, self.surf, gp_Pnt(0, 0, 10))
        # print(proj.Current())
        brep = BRepBuilderAPI_MakeFace(self.surf, poly)
        # brep.Add(poly)
        face = brep.Face()
        face.Location(set_loc(gp_Ax3(), axs))
        return face

    def export_file(self):
        write_step_file(self.compound, self.tempname + ".stp")

    def display_Plane(self):
        self.display.DisplayShape(self.beam_line)
        self.display.DisplayShape(self.compound)
        self.show_axs_pln(scale=1.0)
        self.show()
Esempio n. 7
0
    # obj.show_ball()

    axs = gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(1, 1, 1))
    elp = gen_ellipsoid(axs, [10, 20, 30])
    obj.display.DisplayShape(elp, transparency=0.7, color="BLUE")
    obj.show_axs_pln(axs, scale=20)

    axs = gp_Ax3(gp_Pnt(30, 0, 0), gp_Dir(1, 1, 0))
    elp = gen_ellipsoid(axs, [10, 20, 30])
    obj.display.DisplayShape(elp, transparency=0.7, color="BLUE")
    obj.show_axs_pln(axs, scale=20)

    #elp = gen_ellipsoid_geom(axs, [10, 20, 30])

    top_api = Topo(elp)
    print(top_api.number_of_faces())
    for face in top_api.faces():
        elp_face = face

    print(elp_face)
    elp_surf = BRep_Tool_Surface(elp_face)
    print(elp_surf)

    lin = Geom_Line(gp_Ax1(axs.Location(), gp_Dir(0, 1, 1)))
    api = GeomAPI_IntCS(lin, elp_surf)
    obj.display.DisplayShape(lin)
    print(api.Point(1))
    print(api.Point(2))

    obj.show()