build-dxf 0.0.19 → 0.0.20-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -40,6 +40,36 @@ detailsPoint.set("./json/dp8.json")
40
40
 
41
41
  ```
42
42
 
43
+ # 浏览器编辑功能使用示例
44
+ ```html
45
+
46
+ <template>
47
+ <div class="w-[100vw] h-[100vh]" ref="dom"> </div>
48
+ </template>
49
+
50
+ <style scoped></style>
51
+
52
+ <script setup lang="ts">
53
+ import data from "../data/d9.json"
54
+ import { createEditor } from "build-dxf"
55
+ import { onMounted, ref } from "vue"
56
+ import * as THREE from "three"
57
+ import "build-dxf/index.css"
58
+
59
+ const dom = ref<HTMLDivElement>()
60
+
61
+ onMounted(async ()=>{
62
+ if(dom.value) {
63
+ const camera = new THREE.OrthographicCamera( -100, 100, 100, -100, 0.1, 1000 )
64
+ const { dxfSystem } = await createEditor( dom.value, camera )
65
+ dxfSystem.Dxf.set( (data as any) )
66
+ dxfSystem.Dxf.lineOffset()
67
+ }
68
+ })
69
+ </script>
70
+ ```
71
+
72
+
43
73
 
44
74
  # 浏览器 使用示例
45
75
  ```html
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "build-dxf",
3
- "version": "0.0.19",
3
+ "version": "0.0.20-1",
4
4
  "description": "线段构建双线墙壁的dxf版本",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -35,8 +35,6 @@
35
35
  "dependencies": {
36
36
  "clipper-lib": ">=6.4.2",
37
37
  "dxf-writer": ">=1.18.4",
38
- "three-bvh-csg": ">=0.0.17",
39
- "three-csg-ts": ">=3.2.0",
40
38
  "@tweenjs/tween.js": ">=25.0.0",
41
39
  "vue": ">=3.0.0",
42
40
  "obj2gltf": ">=3.1.6"
package/src/build.d.ts CHANGED
@@ -7,6 +7,28 @@ export * from './utils/DxfSystem/plugin/ModelDataPlugin';
7
7
  * @param camera
8
8
  * @returns
9
9
  */
10
- export declare function createEditor(dom: HTMLDivElement, camera?: THREE.OrthographicCamera): Promise<{
10
+ export declare function createEditor(dom: HTMLDivElement, camera?: THREE.Camera, orbitControls?: boolean, viewPermission?: 'admin'): Promise<{
11
11
  dxfSystem: DxfSystem;
12
+ getFileAll: () => Promise<{
13
+ dxf: File;
14
+ obj: File;
15
+ glb: File;
16
+ gltf: File;
17
+ json: File;
18
+ }>;
12
19
  }>;
20
+ /**
21
+ * 获取所有文件数据
22
+ * @param dxfSystem
23
+ */
24
+ export declare function getFileAll(dxfSystem?: DxfSystem): Promise<{
25
+ dxf: File;
26
+ obj: File;
27
+ glb: File;
28
+ gltf: File;
29
+ json: File;
30
+ }>;
31
+ /** 获取全局DxfSystem
32
+ * @returns
33
+ */
34
+ export declare function getGlobalDxfSystem(): DxfSystem | null;
package/src/build.js CHANGED
@@ -44,6 +44,7 @@ class EventDispatcher extends EventDispatcher$1 {
44
44
  }
45
45
  class Component extends EventDispatcher {
46
46
  parent;
47
+ destroyed = false;
47
48
  constructor(...arg) {
48
49
  super();
49
50
  this.addEventListener("addFromParent", (e) => {
@@ -60,6 +61,7 @@ class Component extends EventDispatcher {
60
61
  onRemoveFromParent(parent) {
61
62
  }
62
63
  destroy() {
64
+ this.destroyed = true;
63
65
  }
64
66
  }
65
67
  class ComponentManager extends EventDispatcher {
@@ -120,7 +122,7 @@ class ComponentManager extends EventDispatcher {
120
122
  * @param callBack
121
123
  */
122
124
  findComponents(predicate) {
123
- return this.components.find(predicate);
125
+ return this.components.filter(predicate);
124
126
  }
125
127
  /**
126
128
  *
@@ -1025,7 +1027,7 @@ class LineSegment {
1025
1027
  return projP1;
1026
1028
  }
1027
1029
  /**
1028
- * 判断线段是与线段相交
1030
+ * 判断线段是否与另一条线段相交(包含共用端点或部分重合的情况)
1029
1031
  * @param line
1030
1032
  */
1031
1033
  intersectLineSegment(line) {
@@ -1033,14 +1035,32 @@ class LineSegment {
1033
1035
  const p2 = this.end;
1034
1036
  const p3 = line.start;
1035
1037
  const p4 = line.end;
1036
- function crossProduct(p12, p22, p32) {
1037
- return (p22.x - p12.x) * (p32.y - p12.y) - (p22.y - p12.y) * (p32.x - p12.x);
1038
+ function crossProduct(a, b, c) {
1039
+ return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
1040
+ }
1041
+ function isPointOnSegment(pt, segStart, segEnd) {
1042
+ return Math.min(segStart.x, segEnd.x) - 1e-10 <= pt.x && pt.x <= Math.max(segStart.x, segEnd.x) + 1e-10 && Math.min(segStart.y, segEnd.y) - 1e-10 <= pt.y && pt.y <= Math.max(segStart.y, segEnd.y) + 1e-10;
1038
1043
  }
1039
1044
  const d1 = crossProduct(p1, p2, p3);
1040
1045
  const d2 = crossProduct(p1, p2, p4);
1041
1046
  const d3 = crossProduct(p3, p4, p1);
1042
1047
  const d4 = crossProduct(p3, p4, p2);
1043
- return d1 * d2 < 0 && d3 * d4 < 0;
1048
+ if (d1 * d2 < 0 && d3 * d4 < 0) {
1049
+ return true;
1050
+ }
1051
+ if (Math.abs(d1) < 1e-10 && isPointOnSegment(p3, p1, p2)) {
1052
+ return true;
1053
+ }
1054
+ if (Math.abs(d2) < 1e-10 && isPointOnSegment(p4, p1, p2)) {
1055
+ return true;
1056
+ }
1057
+ if (Math.abs(d3) < 1e-10 && isPointOnSegment(p1, p3, p4)) {
1058
+ return true;
1059
+ }
1060
+ if (Math.abs(d4) < 1e-10 && isPointOnSegment(p2, p3, p4)) {
1061
+ return true;
1062
+ }
1063
+ return false;
1044
1064
  }
1045
1065
  /**
1046
1066
  * 获取交点
@@ -1455,7 +1475,7 @@ class Dxf extends Component {
1455
1475
  filterLines.push(line2);
1456
1476
  } else filterLines.push(line);
1457
1477
  }
1458
- return filterLines.length > 3 ? linesToPath(this.mergeSameDirectionLine(filterLines)) : [];
1478
+ return filterLines.length > 2 ? linesToPath(this.mergeSameDirectionLine(filterLines)) : [];
1459
1479
  }
1460
1480
  /**
1461
1481
  * 移除短线段
@@ -1478,26 +1498,25 @@ class Dxf extends Component {
1478
1498
  nextline = lines[++i];
1479
1499
  } else break;
1480
1500
  }
1481
- if (nextline) {
1482
- const intersectPoint = preLine.getIntersection(nextline);
1483
- if (intersectPoint) {
1484
- const p0 = preLine.points[1].clone(), p1 = nextline.points[0].clone();
1485
- preLine.points[1].copy(intersectPoint);
1486
- nextline.points[0].copy(intersectPoint);
1487
- if (preLine.length() < this.width) {
1488
- preLine.points[1].copy(p0);
1489
- nextline.points[0].copy(p0);
1490
- } else if (nextline.length() < this.width) {
1491
- preLine.points[1].copy(p1);
1492
- nextline.points[0].copy(p1);
1493
- }
1494
- } else {
1495
- preLine.points[1].copy(nextline.points[0]);
1501
+ if (!nextline) continue;
1502
+ const intersectPoint = preLine.getIntersection(nextline);
1503
+ if (intersectPoint) {
1504
+ const p0 = preLine.points[1].clone(), p1 = nextline.points[0].clone();
1505
+ preLine.points[1].copy(intersectPoint);
1506
+ nextline.points[0].copy(intersectPoint);
1507
+ if (preLine.length() < this.width) {
1508
+ preLine.points[1].copy(p0);
1509
+ nextline.points[0].copy(p0);
1510
+ } else if (nextline.length() < this.width) {
1511
+ preLine.points[1].copy(p1);
1512
+ nextline.points[0].copy(p1);
1496
1513
  }
1497
- filterLines.push(nextline);
1514
+ } else {
1515
+ preLine.points[1].copy(nextline.points[0]);
1498
1516
  }
1517
+ filterLines.push(nextline);
1499
1518
  }
1500
- return filterLines.length > 3 ? linesToPath(filterLines) : [];
1519
+ return filterLines.length > 2 ? linesToPath(filterLines) : [];
1501
1520
  }
1502
1521
  /** 线偏移
1503
1522
  * @description 使用 ClipperLib 对每个点组进行线偏移处理,生成具有指定宽度的墙体路径
@@ -2704,10 +2723,12 @@ class DxfSystem extends ComponentManager {
2704
2723
  return this;
2705
2724
  }
2706
2725
  destroy() {
2707
- this.components.forEach((com) => {
2708
- this.removeComponent(com);
2726
+ [...this.components].forEach((com) => {
2709
2727
  com.destroy();
2710
2728
  });
2729
+ [...this.components].forEach((com) => {
2730
+ this.removeComponent(com);
2731
+ });
2711
2732
  }
2712
2733
  }
2713
2734
  const exporter = new OBJExporter();
@@ -3076,25 +3097,47 @@ function loadRenderPlugin() {
3076
3097
  function loadEditorPlugin() {
3077
3098
  return import("./index3.js");
3078
3099
  }
3079
- async function createEditor(dom, camera) {
3100
+ let gloabalDxfSystem = null;
3101
+ async function createEditor(dom, camera, orbitControls = false, viewPermission) {
3080
3102
  const mp = await Promise.resolve().then(() => index);
3081
3103
  const rp = await loadRenderPlugin();
3082
3104
  const editor = await loadEditorPlugin();
3083
3105
  const dxfSystem = new DxfSystem().usePlugin(mp.ModelDataPlugin.create({
3084
3106
  detailsPoint: false,
3085
- whiteModel: false
3107
+ whiteModel: true
3086
3108
  })).usePlugin(rp.RenderPlugin.create({
3087
3109
  originalLine: false,
3088
3110
  modelData: false,
3089
3111
  detailsPoint: false,
3112
+ orbitControls,
3090
3113
  camera
3091
- })).usePlugin(editor.Editor);
3114
+ })).usePlugin(editor.Editor.create({ viewPermission }));
3092
3115
  const domContainer = dxfSystem.findComponentByType(rp.components.DomContainer);
3093
3116
  domContainer && dom.appendChild(domContainer.domElement);
3117
+ gloabalDxfSystem = dxfSystem;
3094
3118
  return {
3095
- dxfSystem
3119
+ dxfSystem,
3120
+ getFileAll: () => getFileAll(dxfSystem)
3096
3121
  };
3097
3122
  }
3123
+ async function getFileAll(dxfSystem = gloabalDxfSystem) {
3124
+ const whiteModel = dxfSystem.findComponentByName("WhiteModel");
3125
+ const dxf = new File([dxfSystem.Dxf.toDxfBlob()], "dxf.dxf", { type: "application/dxf" });
3126
+ const obj = new File([await whiteModel.toOBJBlob()], "model.obj", { type: "application/octet-stream" });
3127
+ const glb = new File([await whiteModel.toGltfBlob(true)], "model.glb", { type: "application/octet-stream" });
3128
+ const gltf = new File([await whiteModel.toGltfBlob(false)], "model.gltf", { type: "application/json" });
3129
+ const json = new File([JSON.stringify(dxfSystem.Dxf.originalData)], "json.json", { type: "application/json" });
3130
+ return {
3131
+ dxf,
3132
+ obj,
3133
+ glb,
3134
+ gltf,
3135
+ json
3136
+ };
3137
+ }
3138
+ function getGlobalDxfSystem() {
3139
+ return gloabalDxfSystem;
3140
+ }
3098
3141
  export {
3099
3142
  Box2 as B,
3100
3143
  Component as C,
@@ -3109,5 +3152,7 @@ export {
3109
3152
  DetailsPoint as a,
3110
3153
  PointVirtualGrid as b,
3111
3154
  createEditor as c,
3155
+ getGlobalDxfSystem as d,
3156
+ getFileAll as g,
3112
3157
  index$1 as i
3113
3158
  };