示例#1
0
def GetOrientedBoundingBoxShapeCompound(shapes_compound, optimal_OBB=False):

    obb = Bnd_OBB()
    if optimal_OBB:
        is_triangulationUsed = True
        is_optimal = True
        is_shapeToleranceUsed = False
        brepbndlib_AddOBB(shapes_compound, obb, is_triangulationUsed, is_optimal, is_shapeToleranceUsed)
    else:
        brepbndlib_AddOBB(shapes_compound, obb)
    aBaryCenter = obb.Center()
    aXDir = obb.XDirection()
    aYDir = obb.YDirection()
    aZDir = obb.ZDirection()
    aHalfX = obb.XHSize()
    aHalfY = obb.YHSize()
    aHalfZ = obb.ZHSize()

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
    p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())

    pt=[]
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY + az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY + az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY + az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY + az * aHalfZ))

    return  pt
示例#2
0
 def make_axis_triedron(self):
     self.x_axis = AIS_Axis(
         Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(1, 0, 0)))))
     self.y_axis = AIS_Axis(
         Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 1, 0)))))
     self.z_axis = AIS_Axis(
         Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 0, 1)))))
     self.x_axis.SetColor(Quantity_Color(1, 0, 0, Quantity_TOC_RGB))
     self.y_axis.SetColor(Quantity_Color(0, 1, 0, Quantity_TOC_RGB))
     self.z_axis.SetColor(Quantity_Color(0, 0, 1, Quantity_TOC_RGB))
 def test_in_place_operators(self):
     # operator +=
     a = gp_XYZ(1., 2., 3.)
     self.assertEqual(a.X(), 1.)
     self.assertEqual(a.Y(), 2.)
     self.assertEqual(a.Z(), 3.)
     a += gp_XYZ(4., 5., 6.)
     self.assertEqual(a.X(), 5.)
     self.assertEqual(a.Y(), 7.)
     self.assertEqual(a.Z(), 9.)
     # operator *= with a scalar
     b1 = gp_XYZ(2., 4., 5.)
     self.assertEqual(b1.X(), 2.)
     self.assertEqual(b1.Y(), 4.)
     self.assertEqual(b1.Z(), 5.)
     b1 *= 2
     self.assertEqual(b1.X(), 4.)
     self.assertEqual(b1.Y(), 8.)
     self.assertEqual(b1.Z(), 10.)
     # operator *= with a gp_XYZ
     b2 = gp_XYZ(4., 5., 6.)
     self.assertEqual(b2.X(), 4.)
     self.assertEqual(b2.Y(), 5.)
     self.assertEqual(b2.Z(), 6.)
     b2 *= gp_XYZ(3., 6., 7.)
     self.assertEqual(b2.X(), 12.)
     self.assertEqual(b2.Y(), 30.)
     self.assertEqual(b2.Z(), 42.)
     # operator *= with a gp_Mat
     b3 = gp_XYZ(1., 2., 3.)
     self.assertEqual(b3.X(), 1.)
     self.assertEqual(b3.Y(), 2.)
     self.assertEqual(b3.Z(), 3.)
     m_ident = gp_Mat()
     m_ident.SetIdentity()
     b3 *= m_ident
     self.assertEqual(b3.X(), 1.)
     self.assertEqual(b3.Y(), 2.)
     self.assertEqual(b3.Z(), 3.)
     # operator -=
     c = gp_XYZ(3., 2., 1.)
     self.assertEqual(c.X(), 3.)
     self.assertEqual(c.Y(), 2.)
     self.assertEqual(c.Z(), 1.)
     c -= gp_XYZ(1., 0.5, 1.5)
     self.assertEqual(c.X(), 2.)
     self.assertEqual(c.Y(), 1.5)
     self.assertEqual(c.Z(), -0.5)
     # operator /=
     d = gp_XYZ(12., 14., 18.)
     self.assertEqual(d.X(), 12.)
     self.assertEqual(d.Y(), 14.)
     self.assertEqual(d.Z(), 18.)
     d /= 2.
     self.assertEqual(d.X(), 6.)
     self.assertEqual(d.Y(), 7.)
     self.assertEqual(d.Z(), 9.)
 def test_in_place_operators(self) -> None:
     # operator +=
     a = gp_XYZ(1.0, 2.0, 3.0)
     self.assertEqual(a.X(), 1.0)
     self.assertEqual(a.Y(), 2.0)
     self.assertEqual(a.Z(), 3.0)
     a += gp_XYZ(4.0, 5.0, 6.0)
     self.assertEqual(a.X(), 5.0)
     self.assertEqual(a.Y(), 7.0)
     self.assertEqual(a.Z(), 9.0)
     # operator *= with a scalar
     b1 = gp_XYZ(2.0, 4.0, 5.0)
     self.assertEqual(b1.X(), 2.0)
     self.assertEqual(b1.Y(), 4.0)
     self.assertEqual(b1.Z(), 5.0)
     b1 *= 2
     self.assertEqual(b1.X(), 4.0)
     self.assertEqual(b1.Y(), 8.0)
     self.assertEqual(b1.Z(), 10.0)
     # operator *= with a gp_XYZ
     b2 = gp_XYZ(4.0, 5.0, 6.0)
     self.assertEqual(b2.X(), 4.0)
     self.assertEqual(b2.Y(), 5.0)
     self.assertEqual(b2.Z(), 6.0)
     b2 *= gp_XYZ(3.0, 6.0, 7.0)
     self.assertEqual(b2.X(), 12.0)
     self.assertEqual(b2.Y(), 30.0)
     self.assertEqual(b2.Z(), 42.0)
     # operator *= with a gp_Mat
     b3 = gp_XYZ(1.0, 2.0, 3.0)
     self.assertEqual(b3.X(), 1.0)
     self.assertEqual(b3.Y(), 2.0)
     self.assertEqual(b3.Z(), 3.0)
     m_ident = gp_Mat()
     m_ident.SetIdentity()
     b3 *= m_ident
     self.assertEqual(b3.X(), 1.0)
     self.assertEqual(b3.Y(), 2.0)
     self.assertEqual(b3.Z(), 3.0)
     # operator -=
     c = gp_XYZ(3.0, 2.0, 1.0)
     self.assertEqual(c.X(), 3.0)
     self.assertEqual(c.Y(), 2.0)
     self.assertEqual(c.Z(), 1.0)
     c -= gp_XYZ(1.0, 0.5, 1.5)
     self.assertEqual(c.X(), 2.0)
     self.assertEqual(c.Y(), 1.5)
     self.assertEqual(c.Z(), -0.5)
     # operator /=
     d = gp_XYZ(12.0, 14.0, 18.0)
     self.assertEqual(d.X(), 12.0)
     self.assertEqual(d.Y(), 14.0)
     self.assertEqual(d.Z(), 18.0)
     d /= 2.0
     self.assertEqual(d.X(), 6.0)
     self.assertEqual(d.Y(), 7.0)
     self.assertEqual(d.Z(), 9.0)
 def test_in_place_operators(self):
     # operator +=
     a = gp_XYZ(1., 2., 3.)
     self.assertEqual(a.X(), 1.)
     self.assertEqual(a.Y(), 2.)
     self.assertEqual(a.Z(), 3.)
     a += gp_XYZ(4., 5., 6.)
     self.assertEqual(a.X(), 5.)
     self.assertEqual(a.Y(), 7.)
     self.assertEqual(a.Z(), 9.)
     # operator *= with a scalar
     b1 = gp_XYZ(2., 4., 5.)
     self.assertEqual(b1.X(), 2.)
     self.assertEqual(b1.Y(), 4.)
     self.assertEqual(b1.Z(), 5.)
     b1 *= 2
     self.assertEqual(b1.X(), 4.)
     self.assertEqual(b1.Y(), 8.)
     self.assertEqual(b1.Z(), 10.)
     # operator *= with a gp_XYZ
     b2 = gp_XYZ(4., 5., 6.)
     self.assertEqual(b2.X(), 4.)
     self.assertEqual(b2.Y(), 5.)
     self.assertEqual(b2.Z(), 6.)
     b2 *= gp_XYZ(3., 6., 7.)
     self.assertEqual(b2.X(), 12.)
     self.assertEqual(b2.Y(), 30.)
     self.assertEqual(b2.Z(), 42.)
     # operator *= with a gp_Mat
     b3 = gp_XYZ(1., 2., 3.)
     self.assertEqual(b3.X(), 1.)
     self.assertEqual(b3.Y(), 2.)
     self.assertEqual(b3.Z(), 3.)
     m_ident = gp_Mat()
     m_ident.SetIdentity()
     b3 *= m_ident
     self.assertEqual(b3.X(), 1.)
     self.assertEqual(b3.Y(), 2.)
     self.assertEqual(b3.Z(), 3.)
     # operator -=
     c = gp_XYZ(3., 2., 1.)
     self.assertEqual(c.X(), 3.)
     self.assertEqual(c.Y(), 2.)
     self.assertEqual(c.Z(), 1.)
     c -= gp_XYZ(1., 0.5, 1.5)
     self.assertEqual(c.X(), 2.)
     self.assertEqual(c.Y(), 1.5)
     self.assertEqual(c.Z(), -0.5)
     # operator /=
     d = gp_XYZ(12., 14., 18.)
     self.assertEqual(d.X(), 12.)
     self.assertEqual(d.Y(), 14.)
     self.assertEqual(d.Z(), 18.)
     d /= 2.
     self.assertEqual(d.X(), 6.)
     self.assertEqual(d.Y(), 7.)
     self.assertEqual(d.Z(), 9.)
示例#6
0
def get_oriented_boundingbox_ratio(shape, optimal_OBB=True, ratio=1.0):
    """ return the oriented bounding box of the TopoDS_Shape `shape`

    Parameters
    ----------

    shape : TopoDS_Shape or a subclass such as TopoDS_Face
        the shape to compute the bounding box from

    optimal_OBB : bool, True by default. If set to True, compute the
        optimal (i.e. the smallest oriented bounding box). 
        Optimal OBB is a bit longer.

    ratio : float, 1.0 by default.

    Returns
    -------
        a list with center, x, y and z sizes

        a shape
    """
    obb = Bnd_OBB()
    if optimal_OBB:
        is_triangulationUsed = True
        is_optimal = True
        is_shapeToleranceUsed = False
        brepbndlib_AddOBB(shape, obb, is_triangulationUsed, is_optimal,
                          is_shapeToleranceUsed)
    else:
        brepbndlib_AddOBB(shape, obb)

    # converts the bounding box to a shape
    aBaryCenter = obb.Center()
    aXDir = obb.XDirection()
    aYDir = obb.YDirection()
    aZDir = obb.ZDirection()
    aHalfX = obb.XHSize()
    aHalfY = obb.YHSize()
    aHalfZ = obb.ZHSize()
    dx = aHalfX * ratio
    dy = aHalfY * ratio
    dz = aHalfZ * ratio

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
    p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
    anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
    anAxes.SetLocation(gp_Pnt(p.XYZ() - ax * dx - ay * dy - az * dz))
    aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * dx, 2.0 * dy, 2.0 * dz).Shape()
    return aBaryCenter, [dx, dy, dz], aBox
def htransform2(tpnt, tvx, tvy, tvz, pnt, vx, vy, vz):
    tvx = tvy.Crossed(tvz)
    tvy = tvz.Crossed(tvx)
    tvx.Normalize()
    tvy.Normalize()
    tvz.Normalize()
    tpnt = gp_Pnt((gp_Vec(tpnt.XYZ()) + tvz * 0.1).XYZ())
    vx = vy.Crossed(vz)
    vy = vz.Crossed(vx)
    vx.Normalize()
    vy.Normalize()
    vz.Normalize()
    Tt = gp_GTrsf(
        gp_Mat(tvx.X(), tvy.X(), tvz.X(), tvx.Y(), tvy.Y(), tvz.Y(), tvx.Z(),
               tvy.Z(), tvz.Z()), gp_XYZ(tpnt.X(), tpnt.Y(), tpnt.Z()))
    Tt.Invert()
    rott = gp_Mat(tvx.X(), tvy.X(), tvz.X(), tvx.Y(), tvy.Y(), tvz.Y(),
                  tvx.Z(), tvy.Z(), tvz.Z())
    rott.Transpose()
    quatt = gp_Quaternion(rott)
    dispt = gp_Vec(Tt.TranslationPart().X(),
                   Tt.TranslationPart().Y(),
                   Tt.TranslationPart().Z())
    trsft = gp_Trsf()
    trsft.SetTransformation(quatt, dispt)
    rotp = gp_Mat(vx.X(), vy.X(), vz.X(), vx.Y(), vy.Y(), vz.Y(), vx.Z(),
                  vy.Z(), vz.Z())
    quatp = gp_Quaternion(rotp)
    dispp = gp_Vec(gp_Pnt(0, 0, 0), pnt)
    trsfp = gp_Trsf()
    trsfp.SetTransformation(quatp, dispp)
    trsfo = trsfp.Multiplied(trsft)
    loco = TopLoc_Location(trsfo)
    return loco
示例#8
0
 def to_Geom_Line(self):
     return Geom_Line(
         gp_Lin(
             gp_Pnt(0, 0, 0),
             gp_Dir(
                 gp_XYZ(self._coords[0], self._coords[1],
                        self._coords[2]))))
示例#9
0
def get_oriented_boundingbox_coor(shape, optimal_OBB=False):
    '''return the oriented bounding box of the TopoDS_Shape `shape`'''

    obb = Bnd_OBB()
    if optimal_OBB:
        is_triangulationUsed = True
        is_optimal = True
        is_shapeToleranceUsed = False
        brepbndlib_AddOBB(shape, obb, is_triangulationUsed, is_optimal,
                          is_shapeToleranceUsed)
    else:
        brepbndlib_AddOBB(shape, obb)
    # converts the bounding box to a shape
    aBaryCenter = obb.Center()
    aXDir = obb.XDirection()
    aYDir = obb.YDirection()
    aZDir = obb.ZDirection()
    aHalfX = obb.XHSize()
    aHalfY = obb.YHSize()
    aHalfZ = obb.ZHSize()

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
    p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
    anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
    anAxes.SetLocation(
        gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    #corner points on the ground face of the box

    new_origin = gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY -
                        az * aHalfZ)  # left_down
    right_up = gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ)
    right_down = gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ)
    left_up = gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ)
    corners_bot = [new_origin, left_up, right_up, right_down]

    new_origin_top = gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY + az * aHalfZ)
    right_up_top = gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY + az * aHalfZ)
    right_down_top = gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY + az * aHalfZ)
    left_up_top = gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY + az * aHalfZ)
    corners_top = [new_origin_top, left_up_top, right_up_top, right_down_top]

    if corners_top[0].Z() >= corners_bot[0].Z():  #always return bot , top
        return corners_bot, corners_top
    else:
        return corners_top, corners_bot
示例#10
0
def get_oriented_boundingbox(shape, optimal_OBB=True):
    """return the oriented bounding box of the TopoDS_Shape `shape`

    Parameters
    ----------

    shape : TopoDS_Shape or a subclass such as TopoDS_Face
        the shape to compute the bounding box from
    optimal_OBB : bool, True by default. If set to True, compute the
        optimal (i.e. the smallest oriented bounding box). Optimal OBB is
        a bit longer.
    Returns
    -------
        a list with center, x, y and z sizes

        a shape
    """
    obb = Bnd_OBB()
    if optimal_OBB:
        is_triangulation_used = True
        is_optimal = True
        is_shape_tolerance_used = False
        brepbndlib_AddOBB(shape, obb, is_triangulation_used, is_optimal,
                          is_shape_tolerance_used)
    else:
        brepbndlib_AddOBB(shape, obb)

    # converts the bounding box to a shape
    bary_center = obb.Center()
    x_direction = obb.XDirection()
    y_direction = obb.YDirection()
    z_direction = obb.ZDirection()
    a_half_x = obb.XHSize()
    a_half_y = obb.YHSize()
    a_half_z = obb.ZHSize()

    ax = gp_XYZ(x_direction.X(), x_direction.Y(), x_direction.Z())
    ay = gp_XYZ(y_direction.X(), y_direction.Y(), y_direction.Z())
    az = gp_XYZ(z_direction.X(), z_direction.Y(), z_direction.Z())
    p = gp_Pnt(bary_center.X(), bary_center.Y(), bary_center.Z())
    an_axe = gp_Ax2(p, gp_Dir(z_direction), gp_Dir(x_direction))
    an_axe.SetLocation(
        gp_Pnt(p.XYZ() - ax * a_half_x - ay * a_half_y - az * a_half_z))
    a_box = BRepPrimAPI_MakeBox(an_axe, 2.0 * a_half_x, 2.0 * a_half_y,
                                2.0 * a_half_z).Shape()
    return bary_center, [a_half_x, a_half_y, a_half_z], a_box
示例#11
0
def gen_ellipsoid(axs=gp_Ax3(), rxyz=[10, 20, 30]):
    sphere = BRepPrimAPI_MakeSphere(gp_Ax2(), 1).Solid()
    loc = set_loc(gp_Ax3(), axs)
    mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2])
    gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0))
    ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape()
    ellips.Location(loc)
    return ellips
def ConvertBndToShape(theBox):
    aBaryCenter = theBox.Center()
    aXDir = theBox.XDirection()
    aYDir = theBox.YDirection()
    aZDir = theBox.ZDirection()
    aHalfX = theBox.XHSize()
    aHalfY = theBox.YHSize()
    aHalfZ = theBox.ZHSize()

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
    p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
    anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
    anAxes.SetLocation(
        gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY,
                               2.0 * aHalfZ).Shape()
    return aBox
示例#13
0
def gen_ellipsoid_geom(axs=gp_Ax3(), rxyz=[10, 20, 30]):
    sphere = gp_Sphere(axs, 1)
    api = Geom_SphericalSurface(sphere)
    api.Continuity()
    api.Transform()
    print(api.Area())

    loc = set_loc(gp_Ax3(), axs)
    mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2])
    gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0))
    ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape()
    ellips.Location(loc)
    return ellips
示例#14
0
 def __init__(self):
     super().__init__()
     self.axs = gp_Ax3()
     self.radi = [500, 200]
     self.rxyz = [1.5, 1.2, 1.5]
     mat = gp_Mat(self.rxyz[0], 0, 0, 0, self.rxyz[1], 0, 0, 0,
                  self.rxyz[2])
     gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0))
     #self.t = Geom_ToroidalSurface(self.axs, *self.radi)
     self.t = Geom_SphericalSurface(self.axs, 100.0)
     self.face = BRepBuilderAPI_MakeFace(self.t, 1e-6).Face()
     self.face = BRepBuilderAPI_GTransform(self.face, gtrf).Shape()
     self.surf = BRep_Tool.Surface(self.face)
     self.prop = GeomLProp_SLProps(self.surf, 0.0, 0.0, 1, 1.0)
     self.export_stp(self.face)
     print(self.t.UPeriod())
示例#15
0
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_GTransform
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Common

from OCC.Display.SimpleGui import init_display

display, start_display, add_menu, add_function_to_menu = init_display()
orig = gp_Pnt(0., 0., 0.)
sphere = BRepPrimAPI_MakeSphere(orig, 50.).Solid()

# be careful that the following scale numbers are "not too big",
# otherwise boolean operations can be buggy
# see isue
a1 = 17.1
a2 = 17.1
a3 = 3.5
gTrsf = gp_GTrsf(gp_Mat(a1, 0, 0, 0, a2, 0, 0, 0, a3), gp_XYZ(0., 112.2, 0.))
ellipsoid = BRepBuilderAPI_GTransform(sphere, gTrsf).Shape()

# then perform a boolean intersection with a box
box = BRepPrimAPI_MakeBox(gp_Pnt(-1000, -1000, -1000),
                          gp_Pnt(1000, 112.2, 1000)).Shape()
common = BRepAlgoAPI_Common(box, ellipsoid).Shape()
if common.IsNull():
    raise AssertionError("common result is Null.")

display.DisplayShape(box, color="BLACK", transparency=0.8)
display.DisplayShape(ellipsoid, color="BLACK", transparency=0.8)
display.DisplayShape(common, color="BLACK", transparency=0.8, update=True)
start_display()
示例#16
0
文件: trans.py 项目: mirmik/zencad
 def rotation(self):
     xyz = gp_XYZ()
     angle = self._trsf.GetRotation(xyz)[1]
     return zencad.util.vector3(xyz), angle
示例#17
0
def ConvertBndToShape(
    theBox,
    offset=0.0,
    xWidht=None,
    yWidht=None,
    zWidht=None,
    xxWidht=None,
    yyWidht=None,
    zzWidht=None,
):
    aBaryCenter = theBox.Center()
    aXDir = theBox.XDirection()
    aYDir = theBox.YDirection()
    aZDir = theBox.ZDirection()
    aHalfX = theBox.XHSize()
    aHalfY = theBox.YHSize()
    aHalfZ = theBox.ZHSize()

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())

    p = gp_Pnt(aBaryCenter.X() - offset,
               aBaryCenter.Y() - offset,
               aBaryCenter.Z() - offset)
    anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
    anAxes.SetLocation(
        gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    if xWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() + (ax * (aHalfX - xWidht)) - ay * aHalfY -
                   az * aHalfZ))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY + 0.01,
                                   2.0 * aHalfZ + 0.01).Shape()
    elif xxWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() - ax * (3.0 * aHalfX - xxWidht) - ay * aHalfY -
                   az * aHalfZ))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY + 0.01,
                                   2.0 * aHalfZ + 0.01).Shape()
    elif yWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() - ax * aHalfX + ay * (aHalfY - yWidht) -
                   az * aHalfZ))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX + 0.01, 2.0 * aHalfY,
                                   2.0 * aHalfZ + 0.01).Shape()
    elif yyWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() - ax * aHalfX - ay * (3.0 * aHalfY - yyWidht) -
                   az * aHalfZ))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX + 0.01, 2.0 * aHalfY,
                                   2.0 * aHalfZ + 0.01).Shape()
    elif zWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY + az *
                   (aHalfZ - zWidht)))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX + 0.01,
                                   2.0 * aHalfY + 0.01, 2.0 * aHalfZ).Shape()
    elif zzWidht is not None:
        anAxes.SetLocation(
            gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az *
                   (3.0 * aHalfZ - zzWidht)))
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX + 0.01,
                                   2.0 * aHalfY + 0.01, 2.0 * aHalfZ).Shape()
    else:
        aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY,
                                   2.0 * aHalfZ).Shape()

    return aBox
示例#18
0
 def as_xyz(self):
     '''returns a gp_XYZ version of self'''
     return gp_XYZ(*self._pnt.Coord())
示例#19
0
    def write(self, mesh_points, filename, tolerance=None):
        """
        Writes a output file, called `filename`, copying all the structures
        from self.filename but the coordinates. `mesh_points` is a matrix
        that contains the new coordinates to write in the output file.

        :param numpy.ndarray mesh_points: it is a *n_points*-by-3 matrix
            containing the coordinates of the points of the mesh.
        :param str filename: name of the output file.
        :param float tolerance: tolerance for the construction of the faces
            and wires in the write function. If not given it uses
            `self.tolerance`.
        """
        self._check_filename_type(filename)
        self._check_extension(filename)
        self._check_infile_instantiation()

        self.outfile = filename

        if tolerance is not None:
            self.tolerance = tolerance

        # cycle on the faces to update the control points position
        # init some quantities
        faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE)
        n_faces = 0
        control_point_position = self._control_point_position

        compound_builder = BRep_Builder()
        compound = TopoDS_Compound()
        compound_builder.MakeCompound(compound)

        while faces_explorer.More():
            # similar to the parser method
            face = topods_Face(faces_explorer.Current())
            nurbs_converter = BRepBuilderAPI_NurbsConvert(face)
            nurbs_converter.Perform(face)
            nurbs_face = nurbs_converter.Shape()
            face_aux = topods_Face(nurbs_face)
            brep_face = BRep_Tool.Surface(topods_Face(nurbs_face))
            bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
            occ_face = bspline_face

            n_poles_u = occ_face.NbUPoles()
            n_poles_v = occ_face.NbVPoles()

            i = 0
            for pole_u_direction in range(n_poles_u):
                for pole_v_direction in range(n_poles_v):
                    control_point_coordinates = mesh_points[
                        i + control_point_position[n_faces], :]
                    point_xyz = gp_XYZ(*control_point_coordinates)

                    gp_point = gp_Pnt(point_xyz)
                    occ_face.SetPole(pole_u_direction + 1,
                                     pole_v_direction + 1, gp_point)
                    i += 1

            # construct the deformed wire for the trimmed surfaces
            wire_maker = BRepBuilderAPI_MakeWire()
            tol = ShapeFix_ShapeTolerance()
            brep = BRepBuilderAPI_MakeFace(occ_face, self.tolerance).Face()
            brep_face = BRep_Tool.Surface(brep)

            # cycle on the edges
            edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE)
            while edge_explorer.More():
                edge = topods_Edge(edge_explorer.Current())
                # edge in the (u,v) coordinates
                edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux)
                # evaluating the new edge: same (u,v) coordinates, but
                # different (x,y,x) ones
                edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(
                    edge_uv_coordinates[0], brep_face)
                edge_phis_coordinates = edge_phis_coordinates_aux.Edge()
                tol.SetTolerance(edge_phis_coordinates, self.tolerance)
                wire_maker.Add(edge_phis_coordinates)
                edge_explorer.Next()

            # grouping the edges in a wire
            wire = wire_maker.Wire()

            # trimming the surfaces
            brep_surf = BRepBuilderAPI_MakeFace(occ_face, wire).Shape()
            compound_builder.Add(compound, brep_surf)
            n_faces += 1
            faces_explorer.Next()
        self.write_shape_to_file(compound, self.outfile)
示例#20
0
def gen_ellipsoid(axs=gp_Ax3(), rxyz=[10, 20, 30]):
    mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2])
    gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0))
    sphere = BRepPrimAPI_MakeSphere(axs.Ax2(), 1).Solid()
    ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape()
    return ellips
示例#21
0
import numpy as np

from OCC.Core.gp import gp_Ax1, gp_Ax2, gp_Ax3, gp_XY
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Dir, gp_XYZ
from OCC.Core.gp import gp_Trsf, gp_Quaternion

axs = gp_Ax3()

#ax1 = gp_Ax3(gp_Pnt(1, 2, 3), gp_Dir(0.5, 1.0, 1.5))
#ax1 = gp_Ax3(gp_Pnt(1, 2, 3), gp_Dir(1.0, 1.0, 0.0))
ax1 = gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(0.5, 1.0, 1.5))
trf = gp_Trsf()
trf.SetTransformation(ax1, axs)
print(trf)
print(trf.DumpJsonToString())
print(trf.GetRotation(gp_XYZ(*axs.Direction().Coord())))
print(trf.GetRotation(gp_XYZ(*axs.XDirection().Coord())))
print(trf.GetRotation(gp_XYZ(*axs.YDirection().Coord())))

#ax2 = gp_Ax3(gp_Pnt(-1, -2, -3), gp_Dir(0, 0, 1))
#ax2 = gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))
ax2 = gp_Ax3(gp_Pnt(0, 0, 1), gp_Dir(0, 0, 1))
trf2 = gp_Trsf()
trf2.SetTransformation(ax2, axs)
print(trf2.DumpJsonToString())

ax2.Transform(trf)
trf2 = gp_Trsf()
trf2.SetTransformation(ax2, axs)
print(trf2.DumpJsonToString())
                DG.add_edge(i, j + k)
                ntool_loc = htransform2(tool_tcp_pnts[k], tool_tcp_vxs[k],
                                        tool_tcp_vys[k], tool_tcp_vzs[k],
                                        part_pnts[l], part_xdir[l],
                                        part_ydir[l], part_zdir[l])
                ntool_tranf = ntool_loc.Transformation()
                ntool_rot = ntool_tranf.VectorialPart()
                ntool_disp = ntool_tranf.TranslationPart()
                ntool_vx = ntool_rot.Column(1)
                ntool_vx.Normalize()
                ntool_vy = ntool_rot.Column(2)
                ntool_vy.Normalize()
                ntool_vz = ntool_rot.Column(3)
                ntool_vz.Normalize()
                world_ang = abs(
                    math.acos(ntool_vz.Dot(gp_XYZ(
                        0, 0, -1).Normalized()))) / (2 * math.pi)
                world_dist = abs(
                    gp_XYZ(0, ntool_vz.Y(), ntool_vz.Z()).Modulus())
                rel_ang = abs(3 - ntool_vx.Dot(otool_vx) -
                              ntool_vy.Dot(otool_vy) -
                              ntool_vz.Dot(otool_vz)) / (3 * 2 * math.pi)
                rel_dist = abs((ntool_disp - otool_disp).Modulus()) / 1000
                # weigh = rel_dist + rel_ang
                weigh = world_ang  #+ world_dist
                DG[i][j + k]['weight'] = weigh
        print('Graph Genertated')
        toc1 = time.time() - tic1
        print('Graph Generation Time:', toc1)

        # Performing Search
        tic2 = time.time()