Skip to content

Various MText improvements and small fixes #104

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/DxfScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class DxfScene {
/* 0 - CCW, 1 - CW */
this.angDir = this.vars.get("ANGDIR") ?? 0
this.pdSize = this.vars.get("PDSIZE") ?? 0
this.pdMode = this.vars.get("PDMODE") ?? 0
this.isMetric = (this.vars.get("MEASUREMENT") ?? 1) == 1

if(dxf.tables && dxf.tables.layer) {
Expand Down Expand Up @@ -885,7 +886,8 @@ export class DxfScene {
attachment: entity.attachmentPoint,
lineSpacing: entity.lineSpacing,
width: entity.width,
color, layer
color, layer,
columns: entity.columns
})
}

Expand Down Expand Up @@ -1027,12 +1029,6 @@ export class DxfScene {
}

const style = entity.hatchStyle ?? 0

if (style != HatchStyle.ODD_PARITY && style != HatchStyle.THROUGH_ENTIRE_AREA) {
//XXX other styles not yet supported
return
}

const boundaryLoops = this._GetHatchBoundaryLoops(entity)
if (boundaryLoops.length == 0) {
console.warn("HATCH entity with empty boundary loops array " +
Expand Down Expand Up @@ -1983,7 +1979,7 @@ export class DxfScene {
color = ColorCode.BY_BLOCK
} else if (entity.colorIndex === 256) {
color = ColorCode.BY_LAYER
} else if (entity.hasOwnProperty("color")) {
} else if (entity.color) {
color = entity.color
}

Expand Down Expand Up @@ -2125,7 +2121,8 @@ export class DxfScene {
scene.layers.push({
name: layer.name,
displayName: layer.displayName,
color: layer.color
color: layer.color,
visible: layer.visible
})
}

Expand Down
38 changes: 31 additions & 7 deletions src/DxfViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export class DxfViewer {
this.resizeObserver = new ResizeObserver(entries => this._OnResize(entries[0]))
this.resizeObserver.observe(domContainer)
}
domContainer.appendChild(this.canvas)

this.canvas.addEventListener("pointerdown", this._OnPointerEvent.bind(this))
this.canvas.addEventListener("pointerup", this._OnPointerEvent.bind(this))
this.canvas.addEventListener("pointermove", this._OnPointerEvent.bind(this))

this.Render()

Expand Down Expand Up @@ -125,6 +125,8 @@ export class DxfViewer {
}

SetSize(width, height) {
if (width <= 0 || height <= 0) return;

this._EnsureRenderer()

const hScale = width / this.canvasWidth
Expand Down Expand Up @@ -186,7 +188,7 @@ export class DxfViewer {
this.hasMissingChars = scene.hasMissingChars

for (const layer of scene.layers) {
this.layers.set(layer.name, new Layer(layer.name, layer.displayName, layer.color))
this.layers.set(layer.name, new Layer(layer.name, layer.displayName, layer.color, layer.visible))
}

/* Load all blocks on the first pass. */
Expand Down Expand Up @@ -218,6 +220,9 @@ export class DxfViewer {
}

this._Emit("loaded")
if (!this.canvas.parentNode) {
this.domContainer.appendChild(this.canvas)
}

if (scene.bounds) {
this.FitView(scene.bounds.minX - scene.origin.x, scene.bounds.maxX - scene.origin.x,
Expand All @@ -240,14 +245,15 @@ export class DxfViewer {
this.renderer.render(this.scene, this.camera)
}

/** @return {Iterable<{name:String, color:number}>} List of layer names. */
/** @return {Iterable<{name:String, displayName: String, color:number, visible: boolean}>} List of layer names. */
GetLayers() {
const result = []
for (const lyr of this.layers.values()) {
result.push({
name: lyr.name,
displayName: lyr.displayName,
color: this._TransformColor(lyr.color)
color: this._TransformColor(lyr.color),
visible: lyr.visible
})
}
return result
Expand All @@ -259,6 +265,8 @@ export class DxfViewer {
if (!layer) {
return
}

layer.visible = show
for (const obj of layer.objects) {
obj.visible = show
}
Expand Down Expand Up @@ -309,6 +317,8 @@ export class DxfViewer {
this.simpleColorMaterial = null
this.renderer.dispose()
this.renderer = null

this.canvas.remove();
}

SetView(center, width) {
Expand Down Expand Up @@ -374,6 +384,7 @@ export class DxfViewer {
* * "resized" - viewport size changed. Details: {width, height}
* * "pointerdown" - Details: {domEvent, position:{x,y}}, position is in scene coordinates.
* * "pointerup"
* * "pointermove"
* * "viewChanged"
* * "message" - Some message from the viewer. {message: string, level: string}.
*
Expand Down Expand Up @@ -440,18 +451,29 @@ export class DxfViewer {
this._Emit(e.type, {
domEvent: e,
canvasCoord,
position: this._CanvasToSceneCoord(canvasCoord.x, canvasCoord.y)
position: this.CanvasToSceneCoord(canvasCoord.x, canvasCoord.y)
})
}

/** @return {{x,y}} Scene coordinate corresponding to the specified canvas pixel coordinates. */
_CanvasToSceneCoord(x, y) {
CanvasToSceneCoord(x, y) {
if (x < 0 || this.canvasWidth <= x || y < 0 || this.canvasHeight <= y) return null;

const v = new three.Vector3(x * 2 / this.canvasWidth - 1,
-y * 2 / this.canvasHeight + 1,
1).unproject(this.camera)
return {x: v.x, y: v.y}
}

SceneToCanvasCoord(x, y) {
const v = new three.Vector3(x, y, 1).project(this.camera)
const cx = (v.x + 1) * this.canvasWidth / 2;
const cy = (-v.y + 1) * this.canvasHeight / 2;

if (cx < 0 || this.canvasWidth <= cx || cy < 0 || this.canvasHeight <= cy) return null;
return {x: cx, y: cy}
}

_OnResize(entry) {
this.SetSize(Math.floor(entry.contentRect.width), Math.floor(entry.contentRect.height))
}
Expand All @@ -471,6 +493,7 @@ export class DxfViewer {
this.scene.add(obj)
if (layer) {
layer.PushObject(obj)
if (!layer.visible) obj.visible = false
}
}
}
Expand Down Expand Up @@ -924,10 +947,11 @@ class Batch {
}

class Layer {
constructor(name, displayName, color) {
constructor(name, displayName, color, visible) {
this.name = name
this.displayName = displayName
this.color = color
this.visible = visible
this.objects = []
}

Expand Down
44 changes: 40 additions & 4 deletions src/HatchCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class ClipCalculator {
this._CreateNodes()
/* Sort from line start towards end. */
this.nodes.sort((e1, e2) => e1.intersection[0] - e2.intersection[0])
if (this.style == HatchStyle.ODD_PARITY) {
if (this.style === HatchStyle.OUTERMOST) {
return this._GenerateOutermostSegments()
}
if (this.style === HatchStyle.ODD_PARITY) {
return this._GenerateOddParitySegments()
}
//XXX assume through all for all the reset style
Expand Down Expand Up @@ -200,7 +203,7 @@ class ClipCalculator {
if (node.toggle) {
state = !state
}
if (suppress == 0 && state && (node.unsuppress || node.toggle)) {
if (suppress === 0 && state && (node.unsuppress || node.toggle)) {
/* Just started new segment. */
prevNode = node
} else if ((suppress || !state) && prevNode) {
Expand All @@ -214,6 +217,39 @@ class ClipCalculator {
return result
}

_GenerateOutermostSegments() {
const result = []
let state = false
/* Incremented with each suppression, decremented with each un-suppression. */
let suppress = 0
/* Previous node when line was enabled. */
let prevNode = null

for (const node of this.nodes) {
if (node.suppress) {
suppress++
}
if (node.unsuppress) {
suppress--
}
if (node.toggle) {
state = !state
}
if (suppress === 0 && state && (node.unsuppress || node.toggle)) {
/* Just started new segment. */
prevNode = node
} else if ((suppress || !state) && prevNode) {
// Check if the segment is connected to loop 0 to get outer hatch style
if ((node.loopIdx === 0 || prevNode.loopIdx === 0) && node.intersection[0] - prevNode.intersection[0] > Number.EPSILON) {
result.push([prevNode.intersection[0], node.intersection[0]])
}
prevNode = null
}
}

return result
}

_GenerateThroughAllSegments() {
const result = []
/* Incremented with each suppression, decremented with each un-suppression. */
Expand Down Expand Up @@ -266,8 +302,8 @@ class ClipCalculator {
}

export class HatchCalculator {
boundaryLoops
style
boundaryLoops;
style;

/**
* Arrays of `Path` to use as boundary, and each `Path` is array of `Point`.
Expand Down
Loading