RepliCAD

Форк
0
249 строк · 6.3 Кб
1
import {
2
  Geom_CylindricalSurface,
3
  gp_GTrsf2d,
4
  Handle_Geom_Surface,
5
} from "replicad-opencascadejs";
6

7
import { GCWithScope, localGC, WrappingObj } from "./register";
8
import { getOC } from "./oclib.js";
9

10
import { Edge, Face } from "./shapes";
11
import { Plane, makeAx2 } from "./geom";
12
import { axis2d, pnt, Point2D, vec, BoundingBox2d, Curve2D } from "./lib2d";
13

14
export const curvesBoundingBox = (curves: Curve2D[]): BoundingBox2d => {
15
  const oc = getOC();
16
  const boundBox = new oc.Bnd_Box2d();
17

18
  curves.forEach((c) => {
19
    oc.BndLib_Add2dCurve.Add_3(c.wrapped, 1e-6, boundBox);
20
  });
21

22
  return new BoundingBox2d(boundBox);
23
};
24

25
export function curvesAsEdgesOnPlane(curves: Curve2D[], plane: Plane) {
26
  const [r, gc] = localGC();
27
  const ax = r(makeAx2(plane.origin, plane.zDir, plane.xDir));
28

29
  const oc = getOC();
30

31
  const edges = curves.map((curve) => {
32
    const curve3d = oc.GeomLib.To3d(ax, curve.wrapped);
33
    return new Edge(new oc.BRepBuilderAPI_MakeEdge_24(curve3d).Edge());
34
  });
35

36
  gc();
37
  return edges;
38
}
39

40
export const curvesAsEdgesOnSurface = (
41
  curves: Curve2D[],
42
  geomSurf: Handle_Geom_Surface
43
) => {
44
  const [r, gc] = localGC();
45
  const oc = getOC();
46

47
  const modifiedCurves = curves.map((curve) => {
48
    const edgeBuilder = r(
49
      new oc.BRepBuilderAPI_MakeEdge_30(curve.wrapped, geomSurf)
50
    );
51
    return new Edge(edgeBuilder.Edge());
52
  });
53

54
  gc();
55
  return modifiedCurves;
56
};
57

58
export const transformCurves = (
59
  curves: Curve2D[],
60
  transformation: gp_GTrsf2d | null
61
): Curve2D[] => {
62
  const oc = getOC();
63

64
  const modifiedCurves = curves.map((curve) => {
65
    if (!transformation) return curve.clone();
66
    return new Curve2D(oc.GeomLib.GTransform(curve.wrapped, transformation));
67
  });
68

69
  return modifiedCurves;
70
};
71

72
export class Transformation2D extends WrappingObj<gp_GTrsf2d> {
73
  transformCurves(curves: Curve2D[]) {
74
    return transformCurves(curves, this.wrapped);
75
  }
76
}
77

78
export const stretchTransform2d = (
79
  ratio: number,
80
  direction: Point2D,
81
  origin: Point2D = [0, 0]
82
): Transformation2D => {
83
  const oc = getOC();
84
  const axis = axis2d(origin, direction);
85
  const transform = new oc.gp_GTrsf2d_1();
86
  transform.SetAffinity(axis, ratio);
87

88
  axis.delete();
89
  return new Transformation2D(transform);
90
};
91

92
export const translationTransform2d = (
93
  translation: Point2D
94
): Transformation2D => {
95
  const oc = getOC();
96
  const [r, gc] = localGC();
97

98
  const rotation = new oc.gp_Trsf2d_1();
99
  rotation.SetTranslation_1(r(vec(translation)));
100

101
  const transform = new oc.gp_GTrsf2d_2(rotation);
102
  gc();
103
  return new Transformation2D(transform);
104
};
105

106
export const mirrorTransform2d = (
107
  centerOrDirection: Point2D,
108
  origin: Point2D = [0, 0],
109
  mode = "center"
110
): Transformation2D => {
111
  const oc = getOC();
112
  const [r, gc] = localGC();
113

114
  const rotation = new oc.gp_Trsf2d_1();
115
  if (mode === "center") {
116
    rotation.SetMirror_1(r(pnt(centerOrDirection)));
117
  } else {
118
    rotation.SetMirror_2(r(axis2d(origin, centerOrDirection)));
119
  }
120

121
  const transform = new oc.gp_GTrsf2d_2(rotation);
122
  gc();
123
  return new Transformation2D(transform);
124
};
125

126
export const rotateTransform2d = (
127
  angle: number,
128
  center: Point2D = [0, 0]
129
): Transformation2D => {
130
  const oc = getOC();
131
  const [r, gc] = localGC();
132

133
  const rotation = new oc.gp_Trsf2d_1();
134
  rotation.SetRotation(r(pnt(center)), angle);
135

136
  const transform = new oc.gp_GTrsf2d_2(rotation);
137
  gc();
138
  return new Transformation2D(transform);
139
};
140

141
export const scaleTransform2d = (
142
  scaleFactor: number,
143
  center: Point2D = [0, 0]
144
): Transformation2D => {
145
  const oc = getOC();
146
  const [r, gc] = localGC();
147

148
  const scaling = new oc.gp_Trsf2d_1();
149
  scaling.SetScale(r(pnt(center)), scaleFactor);
150

151
  const transform = new oc.gp_GTrsf2d_2(scaling);
152
  gc();
153
  return new Transformation2D(transform);
154
};
155

156
export function faceRadius(face: Face): null | number {
157
  const oc = getOC();
158
  const [r, gc] = localGC();
159
  const geomSurf = r(oc.BRep_Tool.Surface_2(face.wrapped));
160

161
  if (face.geomType !== "CYLINDRE") return null;
162

163
  const cylinder = r((geomSurf.get() as Geom_CylindricalSurface).Cylinder());
164
  const radius = cylinder.Radius();
165
  gc();
166
  return radius;
167
}
168

169
export type ScaleMode = "original" | "bounds" | "native";
170

171
export function curvesAsEdgesOnFace(
172
  curves: Curve2D[],
173
  face: Face,
174
  scale: ScaleMode = "original"
175
) {
176
  const [r, gc] = localGC();
177

178
  const oc = getOC();
179
  let geomSurf = r(oc.BRep_Tool.Surface_2(face.wrapped));
180

181
  const bounds = face.UVBounds;
182

183
  let transformation: null | gp_GTrsf2d = null;
184
  const uAxis = r(axis2d([0, 0], [0, 1]));
185
  const vAxis = r(axis2d([0, 0], [1, 0]));
186

187
  if (scale === "original" && face.geomType !== "PLANE") {
188
    if (face.geomType !== "CYLINDRE")
189
      throw new Error(
190
        "Only planar and cylidrical faces can be unwrapped for sketching"
191
      );
192

193
    const cylinder = r((geomSurf.get() as Geom_CylindricalSurface).Cylinder());
194
    if (!cylinder.Direct()) {
195
      geomSurf = geomSurf.get().UReversed();
196
    }
197
    const radius = cylinder.Radius();
198
    const affinity = stretchTransform2d(1 / radius, [0, 1]);
199
    transformation = affinity.wrapped;
200
  }
201

202
  if (scale === "bounds") {
203
    transformation = r(new oc.gp_GTrsf2d_1());
204
    transformation.SetAffinity(uAxis, bounds.uMax - bounds.uMin);
205

206
    if (bounds.uMin !== 0) {
207
      const translation = r(new oc.gp_GTrsf2d_1());
208
      translation.SetTranslationPart(new oc.gp_XY_2(0, -bounds.uMin));
209
      transformation.Multiply(translation);
210
    }
211

212
    const vTransformation = r(new oc.gp_GTrsf2d_1());
213
    vTransformation.SetAffinity(vAxis, bounds.vMax - bounds.vMin);
214
    transformation.Multiply(vTransformation);
215

216
    if (bounds.vMin !== 0) {
217
      const translation = r(new oc.gp_GTrsf2d_1());
218
      translation.SetTranslationPart(r(new oc.gp_XY_2(0, -bounds.vMin)));
219
      transformation.Multiply(translation);
220
    }
221
  }
222

223
  const modifiedCurves = transformCurves(curves, transformation);
224
  const edges = curvesAsEdgesOnSurface(modifiedCurves, geomSurf);
225

226
  gc();
227
  return edges;
228
}
229

230
export function edgeToCurve(e: Edge, face: Face): Curve2D {
231
  const oc = getOC();
232
  const r = GCWithScope();
233

234
  const adaptor = r(new oc.BRepAdaptor_Curve2d_2(e.wrapped, face.wrapped));
235

236
  const trimmed = new oc.Geom2d_TrimmedCurve(
237
    adaptor.Curve(),
238
    adaptor.FirstParameter(),
239
    adaptor.LastParameter(),
240
    true,
241
    true
242
  );
243

244
  if (e.orientation === "backward") {
245
    trimmed.Reverse();
246
  }
247

248
  return new Curve2D(new oc.Handle_Geom2d_Curve_2(trimmed));
249
}
250

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.