diff --git a/web/app/3d/craft/brep/wizards/preview-wizard.js b/web/app/3d/craft/brep/wizards/preview-wizard.js index 4fc09bb1..9c67e3b6 100644 --- a/web/app/3d/craft/brep/wizards/preview-wizard.js +++ b/web/app/3d/craft/brep/wizards/preview-wizard.js @@ -11,21 +11,26 @@ export class PreviewWizard extends Wizard { this.previewObject = null; this.app.viewer.workGroup.add(this.previewGroup); this.updatePreview(); + app.bus.subscribe('refreshSketch', this.onSketchUpdate); } + onSketchUpdate = () => { + this.updatePreview(); + }; + createPreviewObject() {throw 'abstract'}; updatePreview() { this.destroyPreviewObject(); this.previewObject = this.createPreviewObject(this.app, this.readFormFields()); - if (this.previewObject != null) { + if (this.previewObject !== null) { this.previewGroup.add( this.previewObject ); } this.app.viewer.render(); } destroyPreviewObject() { - if (this.previewObject != null) { + if (this.previewObject !== null) { this.previewGroup.remove( this.previewObject ); this.previewObject.geometry.dispose(); this.previewObject = null; @@ -38,6 +43,7 @@ export class PreviewWizard extends Wizard { } dispose() { + this.app.bus.unsubscribe('refreshSketch', this.onSketchUpdate); this.destroyPreviewObject(); this.app.viewer.workGroup.remove(this.previewGroup); this.app.viewer.render(); @@ -75,17 +81,7 @@ export class SketchBasedPreviewer { create(app, params) { const face = app.findFace(params.face); if (!face) return null; - const needSketchRead = !this.sketch || params.face != this.face; - if (needSketchRead) { - this.sketch = ReadSketchFromFace(app, face); - //for (let polygon of this.sketch) { - //if (!Loop.isPolygonCCWOnSurface(polygon, face.surface()) && this.fixToCCW) { - // polygon.reverse(); - //} - //} - this.face = params.face; - } - const triangles = this.createImpl(app, params, this.sketch.fetchContours(), face); + const triangles = this.createImpl(app, params, face.sketch.fetchContours(), face); return PreviewWizard.createMesh(triangles); } } diff --git a/web/app/3d/craft/brep/wizards/wizard.js b/web/app/3d/craft/brep/wizards/wizard.js index ea8b380d..1798dd67 100644 --- a/web/app/3d/craft/brep/wizards/wizard.js +++ b/web/app/3d/craft/brep/wizards/wizard.js @@ -9,7 +9,7 @@ export class Wizard { this.formFields = {}; this.box = this.createUI(opearation, metadata); this.overridingHistory = false; - if (initialState != undefined) { + if (initialState !== undefined) { this.setFormFields(initialState); } } diff --git a/web/app/3d/craft/sketch/sketch-reader.js b/web/app/3d/craft/sketch/sketch-reader.js index f378a522..bf458b72 100644 --- a/web/app/3d/craft/sketch/sketch-reader.js +++ b/web/app/3d/craft/sketch/sketch-reader.js @@ -15,7 +15,7 @@ class SketchGeom { } fetchContours() { - if (this._contours == null) { + if (this._contours === null) { this._contours = FetchContours(this); } return this._contours; @@ -91,7 +91,9 @@ export function ReadSketchPoint(arr) { export function ReadSketchFromFace(app, face) { const savedFace = localStorage.getItem(app.faceStorageKey(face.id)); - if (savedFace == null) return null; + if (savedFace === null) { + return null; + } return ReadSketch(JSON.parse(savedFace), face.id, true); } diff --git a/web/app/3d/modeler-app.js b/web/app/3d/modeler-app.js index 7bc5cf25..2e02d4c3 100644 --- a/web/app/3d/modeler-app.js +++ b/web/app/3d/modeler-app.js @@ -622,12 +622,7 @@ App.prototype.findSketches = function(solid) { }; App.prototype.refreshSketchOnFace = function(sketchFace) { - var faceStorageKey = this.faceStorageKey(sketchFace.id); - var savedFace = localStorage.getItem(faceStorageKey); - if (savedFace != null) { - var geom = ReadSketch(JSON.parse(savedFace), sketchFace.id, true); - sketchFace.syncSketches(geom); - } + sketchFace.updateSketch(this); }; App.prototype.save = function() { diff --git a/web/app/3d/scene/scene-object.js b/web/app/3d/scene/scene-object.js index 8213696a..a2f07207 100644 --- a/web/app/3d/scene/scene-object.js +++ b/web/app/3d/scene/scene-object.js @@ -4,6 +4,7 @@ import Counters from '../counters' import {Matrix3, BasisForPlane} from '../../math/l3space' import {isCurveClass} from '../cad-utils' import DPR from '../../utils/dpr' +import {ReadSketch, ReadSketchFromFace} from "../craft/sketch/sketch-reader"; export class SceneSolid { @@ -22,6 +23,8 @@ export class SceneSolid { this.mergeable = true; this.sceneFaces = []; + this.sketch = null; + this.material = createSolidMaterial(skin); } @@ -113,8 +116,24 @@ export class SceneFace { threeFace.__TCAD_SceneFace = this; } - syncSketches(geom) { - if (this.sketch3DGroup != null) { + readSketchGeom(app) { + let faceStorageKey = app.faceStorageKey(this.id); + let savedFace = localStorage.getItem(faceStorageKey); + if (savedFace === null) { + return null; + } + return ReadSketch(JSON.parse(savedFace), this.id, true); + } + + updateSketch(app) { + this.sketch = this.readSketchGeom(app); + if (this.sketch !== null) { + this.syncSketch(this.sketch); + } + } + + syncSketch(geom) { + if (this.sketch3DGroup !== null) { for (let i = this.sketch3DGroup.children.length - 1; i >= 0; --i) { this.sketch3DGroup.remove(this.sketch3DGroup.children[i]); } @@ -123,10 +142,9 @@ export class SceneFace { this.solid.cadGroup.add(this.sketch3DGroup); } - const basis = this.basis(); - const _3dTransformation = new Matrix3().setBasis(basis); - //we lost depth or z off in 2d sketch, calculate it again - const depth = this.depth(); + let surface = this.surface(); + let [u, v] = surface.middle(); + const _3dTransformation = surface.tangentPlane(u, v).get3DTransformation(); const addSketchObjects = (sketchObjects, material, close) => { for (let sketchObject of sketchObjects) { let line = new THREE.Line(undefined, material); @@ -134,7 +152,6 @@ export class SceneFace { const chunks = sketchObject.approximate(10); function addLine(p, q) { const lg = line.geometry; - chunks[p].z = chunks[q].z = depth; const a = _3dTransformation.apply(chunks[p]); const b = _3dTransformation.apply(chunks[q]); @@ -153,7 +170,7 @@ export class SceneFace { } findById(sketchObjectId) { - return this.sketch3DGroup.children.find(o => o.__TCAD_SketchObject && o.__TCAD_SketchObject.id == sketchObjectId); + return this.sketch3DGroup.children.find(o => o.__TCAD_SketchObject && o.__TCAD_SketchObject.id === sketchObjectId); } getSketchObjectVerticesIn3D(sketchObjectId) { diff --git a/web/app/brep/geom/impl/nurbs.js b/web/app/brep/geom/impl/nurbs.js index 3a31cab3..c357b029 100644 --- a/web/app/brep/geom/impl/nurbs.js +++ b/web/app/brep/geom/impl/nurbs.js @@ -291,6 +291,15 @@ export class NurbsSurface extends Surface { this.simpleSurface = simpleSurface || figureOutSimpleSurface(this); } + middle() { + let {min: uMin, max: uMax} = this.verb.domainU(); + let {min: vMin, max: vMax} = this.verb.domainV(); + return [ + (uMax - uMin) * 0.5, + (vMax - vMin) * 0.5 + ]; + } + toNurbs() { return this; } diff --git a/web/app/brep/geom/impl/plane.js b/web/app/brep/geom/impl/plane.js index ce23e425..41a2c6cc 100644 --- a/web/app/brep/geom/impl/plane.js +++ b/web/app/brep/geom/impl/plane.js @@ -46,7 +46,7 @@ export class Plane extends Surface { if (!this.__3dTr) { const basis = new Matrix3().setBasis(this.basis()); const translate = new Matrix3(); - translate.tz = this.w + translate.tz = this.w; this.__3dTr = basis.combine(translate); // this.__3dTr.tz = this.w; }