cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

Scaling a STEP file in one dimension

Open hexad opened this issue 2 years ago • 11 comments

I want to perform a scaling operation to a STEP file that i imported.

The problem is, i want to be scaled only on one dimension. Searching the docs (https://cadquery.readthedocs.io/en/latest/classreference.html), i found the scale() function.

However, looking at the source code,

    def scale(self, factor: float) -> "Shape":
        """
        Scales this shape through a transformation.
        """

        T = gp_Trsf()
        T.SetScale(gp_Pnt(), factor)

        return self._apply_transform(T)

it does not accept axis information.

So, is it possible to scale an item only on one axis?

hexad avatar Mar 10 '22 16:03 hexad

Maybe i can do this using a scaling matrix?

t = cq.Matrix([
        [x, 0, 0, 0],
        [0, y, 0, 0],
        [0, 0, z, 0],
        [0, 0, 0, 1]
    ])

Any help with how this can be incorporated?

hexad avatar Mar 11 '22 10:03 hexad

There is some code in this issue that might help you.

jmwright avatar Mar 12 '22 13:03 jmwright

Thanks. I tried the code there, but CadQuery has trouble importing the libraries. I get ModuleNotFoundError: No module name 'OCC'. I am using the CQ-Editor, standalone application, so how come OCC is not recognized?

hexad avatar Mar 14 '22 13:03 hexad

Those references should be to OCP now since we've moved away from PythonOCC.

import cadquery as cq

from OCP.gp import gp_Mat, gp_Trsf, gp_GTrsf
# from OCP.Precision import precision_Angular, precision_Confusion
from OCP.BRepBuilderAPI import (BRepBuilderAPI_GTransform,
                                     BRepBuilderAPI_Transform)
from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox

z_delta = -20
xs = 4
#scaleM = cq.Matrix([
#[ xs, 0.0, 0.0, 0.0],
#[0.0, 1.0, 0.0, 0.0],
#[0.0, 0.0, 1.0, 0.0]])
R = cq.Solid.makeBox(10,10,10)
# R1 = R.transformGeometry(scaleM)
#R1 = cq.Shape.cast(BRepBuilderAPI_GTransform(R.wrapped, gp_GTrsf(scaleM.wrapped), True).Shape())
#show_object(R1)
#show_object(R.translate((50,0,0)))

#shape = BRepPrimAPI_MakeBox(10., 10., 10.).Shape()
xform = gp_GTrsf()
xform.SetVectorialPart(gp_Mat(
xs, 0, 0,
0, 1, 0,
0, 0, 1,
))
brep = BRepBuilderAPI_GTransform(R.wrapped, xform, False)
brep.Build()
fshape = brep.Shape()
R1 = cq.Shape.cast(fshape)
show_object(R1)

jmwright avatar Mar 14 '22 18:03 jmwright

Thank you! How exactly do i select the axis and where do i change the scaling factor?

hexad avatar Mar 15 '22 14:03 hexad

The xs is the scaling factor and its location on the identity matrix changes the scaling axis?

hexad avatar Mar 15 '22 14:03 hexad

The xs is the scaling factor and its location on the identity matrix changes the scaling axis?

See this https://en.wikipedia.org/wiki/Scaling_(geometry) and this https://en.wikipedia.org/wiki/Affine_transformation

fedorkotov avatar Mar 15 '22 15:03 fedorkotov

I tried this:

imported_STEP = cq.importers.importStep('./CQ/model.STEP')
xs = 4
xform = gp_GTrsf()
xform.SetVectorialPart(gp_Mat(
xs, 0, 0,
0, 1, 0,
0, 0, 1,
))

brep = BRepBuilderAPI_GTransform(imported_STEP.wrapped, xform, False)
brep.Build()
fshape = brep.Shape()
R1 = cq.Shape.cast(fshape)
show_object(R1)

but i get this error: AttributeError: 'Workplane' object has no attribute 'warped'

hexad avatar Mar 15 '22 16:03 hexad

Are you sure that the error says warped? It should say wrapped. I think you want imported_STEP.wrapped to become imported_STEP.val().wrapped

jmwright avatar Mar 15 '22 18:03 jmwright

You were right, it was wrapped. Thank you very much! This worked. As far as i can tell, CQ can run all the functions of the OpenCascade library? Do you have any good documentation reference for them? Once again, thanks a lot!

hexad avatar Mar 15 '22 20:03 hexad

For the OCP layer, I rely on the OpenCASCADE documentation. https://dev.opencascade.org/doc/overview/html/

jmwright avatar Mar 16 '22 00:03 jmwright