Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2D dimension #235

Closed
wants to merge 12 commits into from
40 changes: 38 additions & 2 deletions viewer/src/components/display/dimensions/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import { IfcComponent } from '../../../base-types';
import { IfcDimensionLine } from './dimension-line';
import { IfcContext } from '../../context';

type DimensionUnits = "m" | "mm";

export class IfcDimensions extends IfcComponent {
private readonly context: IfcContext;
private dimensions: IfcDimensionLine[] = [];
private currentDimension?: IfcDimensionLine;
private currentDimensionIn2D?: IfcDimensionLine;
readonly labelClassName = 'ifcjs-dimension-label';
readonly previewClassName = 'ifcjs-dimension-preview';

Expand All @@ -30,6 +33,7 @@ export class IfcDimensions extends IfcComponent {
// Measures

private baseScale = new Vector3(1, 1, 1);
private dimensionIn2D = false;

// Geometries
private endpoint: BufferGeometry;
Expand Down Expand Up @@ -65,6 +69,7 @@ export class IfcDimensions extends IfcComponent {
this.dimensions.forEach((dim) => dim.dispose());
(this.dimensions as any) = null;
(this.currentDimension as any) = null;
(this.currentDimensionIn2D as any) = null;
this.endpoint.dispose();
(this.endpoint as any) = null;

Expand Down Expand Up @@ -191,13 +196,16 @@ export class IfcDimensions extends IfcComponent {
}

cancelDrawing() {
if (!this.currentDimension) return;
if (!this.currentDimension || !this.currentDimensionIn2D) return;
this.dragging = false;
this.currentDimension?.removeFromScene();
this.currentDimension = undefined;

this.currentDimensionIn2D?.removeFromScene();
this.currentDimensionIn2D = undefined;
}

setDimensionUnit(units: string) {
setDimensionUnit(units: DimensionUnits) {
if (!units) return;
if (units === "mm") {
IfcDimensionLine.units = units;
Expand All @@ -208,6 +216,10 @@ export class IfcDimensions extends IfcComponent {
}
}

set setDimensionIn2D(state: boolean) {
this.dimensionIn2D = state;
}

private drawStart() {
this.dragging = true;
const intersects = this.context.castRayIfc();
Expand All @@ -232,13 +244,24 @@ export class IfcDimensions extends IfcComponent {
if (!found) return;
this.endPoint = found;
if (!this.currentDimension) this.currentDimension = this.drawDimension();

this.currentDimension.endPoint = this.endPoint;
}

private drawEnd() {
if (!this.currentDimension) return;
this.currentDimension.createBoundingBox();
this.dimensions.push(this.currentDimension);

if (this.dimensionIn2D) {
if (!this.currentDimensionIn2D) this.currentDimensionIn2D = this.draw2DDimension();
this.currentDimensionIn2D.endPoint = this.endPoint.setY(this.startPoint.y);
this.currentDimensionIn2D.createBoundingBox();
this.dimensions.push(this.currentDimensionIn2D);
this.currentDimensionIn2D = undefined;
this.currentDimension?.removeFromScene();
}

this.currentDimension = undefined;
this.dragging = false;
}
Expand All @@ -260,6 +283,19 @@ export class IfcDimensions extends IfcComponent {
);
}

private draw2DDimension() {
return new IfcDimensionLine(
this.context,
this.startPoint,
this.endPoint,
this.lineMaterial,
this.endpointsMaterial,
this.endpoint,
this.labelClassName,
this.baseScale
);
}

private getBoundingBoxes() {
return this.dimensions
.map((dim) => dim.boundingBox)
Expand Down
Loading