def _calcTransforms(self): """Computes transformation matrices to convert between coordinates Computes transformation matrices to convert between local and global coordinates. """ # r is the forward transformation matrix from world to local coordinates # ok i will be really honest, i cannot understand exactly why this works # something bout the order of the translation and the rotation. # the double-inverting is strange, and I don't understand it. forward = Matrix() inverse = Matrix() forwardT = gp_Trsf() inverseT = gp_Trsf() global_coord_system = gp_Ax3() local_coord_system = gp_Ax3( gp_Pnt(*self.origin.toTuple()), gp_Dir(*self.zDir.toTuple()), gp_Dir(*self.xDir.toTuple()), ) forwardT.SetTransformation(global_coord_system, local_coord_system) forward.wrapped = gp_GTrsf(forwardT) inverseT.SetTransformation(local_coord_system, global_coord_system) inverse.wrapped = gp_GTrsf(inverseT) self.lcs = local_coord_system self.rG = inverse self.fG = forward
def __init__(self, *args): T = gp_Trsf() if len(args) == 0: pass elif len(args) == 1: t = args[0] if isinstance(t, Vector): T.SetTranslationPart(t.wrapped) elif isinstance(t, Plane): cs = gp_Ax3(t.origin.toPnt(), t.zDir.toDir(), t.xDir.toDir()) T.SetTransformation(cs) T.Invert() elif isinstance(t, TopLoc_Location): self.wrapped = t return elif isinstance(t, gp_Trsf): T = t elif len(args) == 2: t, v = args cs = gp_Ax3(v.toPnt(), t.zDir.toDir(), t.xDir.toDir()) T.SetTransformation(cs) T.Invert() else: t, ax, angle = args T.SetRotation(gp_Ax1(Vector().toPnt(), ax.toDir()), angle * math.pi / 180.0) T.SetTranslationPart(t.wrapped) self.wrapped = TopLoc_Location(T)
def __init__( self, origin: Union[Tuple[float, float, float], Vector], xDir: Optional[Union[Tuple[float, float, float], Vector]] = None, normal: Union[Tuple[float, float, float], Vector] = (0, 0, 1), ): """ Create a Plane with an arbitrary orientation :param origin: the origin in global coordinates :param xDir: an optional vector representing the xDirection. :param normal: the normal direction for the plane :raises ValueError: if the specified xDir is not orthogonal to the provided normal """ zDir = Vector(normal) if zDir.Length == 0.0: raise ValueError("normal should be non null") self.zDir = zDir.normalized() if xDir is None: ax3 = gp_Ax3(Vector(origin).toPnt(), Vector(normal).toDir()) xDir = Vector(ax3.XDirection()) else: xDir = Vector(xDir) if xDir.Length == 0.0: raise ValueError("xDir should be non null") self._setPlaneDir(xDir) self.origin = Vector(origin)
def handleSelection(self): inspected_items = self.inspected_items self.sigRemoveObjects.emit(inspected_items) inspected_items.clear() items = self.selectedItems() if len(items) == 0: return item = items[-1] if type(item) is CQStackItem: cq_plane = item.workplane.plane dim = item.workplane.largestDimension() plane = gp_Ax3(cq_plane.origin.toPnt(), cq_plane.zDir.toDir(), cq_plane.xDir.toDir()) self.sigChangePlane.emit(plane) self.sigShowPlane[bool, float].emit(True, dim) for child in (item.child(i) for i in range(item.childCount())): obj = child.cq_item if hasattr(obj, 'wrapped') and type(obj) != Vector: ais = AIS_ColoredShape(obj.wrapped) inspected_items.append(ais) else: self.sigShowPlane.emit(False) obj = item.cq_item if hasattr(obj, 'wrapped') and type(obj) != Vector: ais = AIS_ColoredShape(obj.wrapped) inspected_items.append(ais) self.sigDisplayObjects.emit(inspected_items, False)
def __init__(self, *args): T = gp_Trsf() if len(args) == 0: pass elif len(args) == 1: t = args[0] if isinstance(t, Vector): T.SetTranslationPart(t.wrapped) elif isinstance(t, Plane): cs = gp_Ax3(t.origin.toPnt(), t.zDir.toDir(), t.xDir.toDir()) T.SetTransformation(cs) T.Invert() elif isinstance(t, TopLoc_Location): self.wrapped = t return elif isinstance(t, gp_Trsf): T = t elif isinstance(t, (tuple, list)): raise TypeError( "A tuple or list is not a valid parameter, use a Vector instead." ) else: raise TypeError("Unexpected parameters") elif len(args) == 2: t, v = args cs = gp_Ax3(v.toPnt(), t.zDir.toDir(), t.xDir.toDir()) T.SetTransformation(cs) T.Invert() else: t, ax, angle = args T.SetRotation(gp_Ax1(Vector().toPnt(), ax.toDir()), angle * math.pi / 180.0) T.SetTranslationPart(t.wrapped) self.wrapped = TopLoc_Location(T)
def mirrorInPlane(self, listOfShapes, axis="X"): local_coord_system = gp_Ax3(self.origin.toPnt(), self.zDir.toDir(), self.xDir.toDir()) T = gp_Trsf() if axis == "X": T.SetMirror( gp_Ax1(self.origin.toPnt(), local_coord_system.XDirection())) elif axis == "Y": T.SetMirror( gp_Ax1(self.origin.toPnt(), local_coord_system.YDirection())) else: raise NotImplementedError resultWires = [] for w in listOfShapes: mirrored = w.transformShape(Matrix(T)) # attemp stitching of the wires resultWires.append(mirrored) return resultWires
def toPln(self) -> gp_Pln: return gp_Pln( gp_Ax3(self.origin.toPnt(), self.zDir.toDir(), self.xDir.toDir()))