|
@@ -25,16 +25,20 @@ import { ImageData } from "../components/image";
|
|
|
import { onlyId } from "@/utils/shared";
|
|
|
import { ArrowData, PointerPosition } from "../components/arrow";
|
|
|
import { IconData } from "../components/icon";
|
|
|
-import { useSetViewport, useViewer, useViewerInvertTransform } from "./use-viewer";
|
|
|
+import {
|
|
|
+ useSetViewport,
|
|
|
+ useViewer,
|
|
|
+ useViewerInvertTransform,
|
|
|
+} from "./use-viewer";
|
|
|
import { nextTick } from "vue";
|
|
|
import { SLineData } from "../components/sequent-line";
|
|
|
|
|
|
export const useGetDXF = () => {
|
|
|
const store = useStore();
|
|
|
const stage = useStage();
|
|
|
- const invMat = useViewerInvertTransform()
|
|
|
- const { setViewport } = useSetViewport()
|
|
|
- const {viewer} = useViewer()
|
|
|
+ const invMat = useViewerInvertTransform();
|
|
|
+ const { setViewport } = useSetViewport();
|
|
|
+ const { viewer } = useViewer();
|
|
|
|
|
|
return async () => {
|
|
|
const writer = new DxfWriter();
|
|
@@ -42,8 +46,8 @@ export const useGetDXF = () => {
|
|
|
const zip = new Zip();
|
|
|
const genPromises: Promise<any>[] = [];
|
|
|
|
|
|
- const fontStyle = writer.tables.styleTable.records[0]
|
|
|
- fontStyle.fontFileName = 'SimSun'
|
|
|
+ const fontStyle = writer.tables.styleTable.records[0];
|
|
|
+ fontStyle.fontFileName = "SimSun";
|
|
|
|
|
|
type PL = {
|
|
|
id?: string;
|
|
@@ -86,46 +90,48 @@ export const useGetDXF = () => {
|
|
|
const mat = $text.getTransform();
|
|
|
const fontSize = $text.fontSize() * 0.8;
|
|
|
let text = $text.text();
|
|
|
- text = text.replace(/(\n|\r|\t)/ig, '')
|
|
|
+ text = text.replace(/(\n|\r|\t)/gi, "");
|
|
|
const pad = $text.padding();
|
|
|
const align = $text.align();
|
|
|
- const textArr: {content: string, charCount: number}[] = [];
|
|
|
+ const textArr: { content: string; charCount: number }[] = [];
|
|
|
let lineNum = 1;
|
|
|
const width = $text.width();
|
|
|
const charWidth = fontSize * 0.9;
|
|
|
|
|
|
- console.log(text)
|
|
|
- const lineCharCount = sp ? Math.max(Math.floor(width / charWidth), 2) : Number.MAX_VALUE;
|
|
|
+ console.log(text);
|
|
|
+ const lineCharCount = sp
|
|
|
+ ? Math.max(Math.floor(width / charWidth), 2)
|
|
|
+ : Number.MAX_VALUE;
|
|
|
let ndx = 0;
|
|
|
let prevNdx = 0;
|
|
|
- let charCount = 0
|
|
|
+ let charCount = 0;
|
|
|
|
|
|
while (ndx < text.length) {
|
|
|
- ndx++
|
|
|
- const c = /[\u4e00-\u9fff]/.test(text.charAt(ndx)) ? 2.4 : 1
|
|
|
-
|
|
|
+ ndx++;
|
|
|
+ const c = /[\u4e00-\u9fff]/.test(text.charAt(ndx)) ? 2.4 : 1;
|
|
|
+
|
|
|
if (charCount === lineCharCount || ndx >= text.length) {
|
|
|
- charCount += c
|
|
|
+ charCount += c;
|
|
|
textArr.push({
|
|
|
content: text.substring(prevNdx, ndx + 1),
|
|
|
- charCount
|
|
|
+ charCount,
|
|
|
});
|
|
|
- charCount = 0
|
|
|
- prevNdx = ndx
|
|
|
+ charCount = 0;
|
|
|
+ prevNdx = ndx;
|
|
|
} else if (charCount > lineCharCount) {
|
|
|
textArr.push({
|
|
|
content: text.substring(prevNdx, ndx),
|
|
|
- charCount
|
|
|
+ charCount,
|
|
|
});
|
|
|
- charCount = 0
|
|
|
- ndx--
|
|
|
- prevNdx = ndx
|
|
|
+ charCount = 0;
|
|
|
+ ndx--;
|
|
|
+ prevNdx = ndx;
|
|
|
} else {
|
|
|
- charCount += c
|
|
|
+ charCount += c;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- console.log(textArr)
|
|
|
+ console.log(textArr);
|
|
|
textArr.forEach((item) => {
|
|
|
const lineWidth = charWidth * item.charCount;
|
|
|
let p = { x: pad, y: pad + lineNum * fontSize * 1.2 };
|
|
@@ -143,37 +149,42 @@ export const useGetDXF = () => {
|
|
|
// : align === "right"
|
|
|
// ? TextHorizontalAlignment.Right
|
|
|
// : TextHorizontalAlignment.Left,
|
|
|
- })
|
|
|
- const text = writer.addText(point3d(start.x, -start.y), fontSize, item.content, {
|
|
|
- rotation: $text.rotation(),
|
|
|
- // horizontalAlignment:
|
|
|
- // align === "center"
|
|
|
- // ? TextHorizontalAlignment.Center
|
|
|
- // : align === "right"
|
|
|
- // ? TextHorizontalAlignment.Right
|
|
|
- // : TextHorizontalAlignment.Left,
|
|
|
});
|
|
|
- text.trueColor = TrueColor.fromHex($text.fill() as string).toString()
|
|
|
- text.height = fontSize
|
|
|
+ const text = writer.addText(
|
|
|
+ point3d(start.x, -start.y),
|
|
|
+ fontSize,
|
|
|
+ item.content,
|
|
|
+ {
|
|
|
+ rotation: $text.rotation(),
|
|
|
+ // horizontalAlignment:
|
|
|
+ // align === "center"
|
|
|
+ // ? TextHorizontalAlignment.Center
|
|
|
+ // : align === "right"
|
|
|
+ // ? TextHorizontalAlignment.Right
|
|
|
+ // : TextHorizontalAlignment.Left,
|
|
|
+ }
|
|
|
+ );
|
|
|
+ text.trueColor = TrueColor.fromHex($text.fill() as string).toString();
|
|
|
+ text.height = fontSize;
|
|
|
lineNum++;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
const writeImage = async (imgGroup: Group, minSize: Size) => {
|
|
|
- let rect = imgGroup.getClientRect()
|
|
|
+ let rect = imgGroup.getClientRect();
|
|
|
const oldViewMat = viewer.viewMat;
|
|
|
- setViewport(rect)
|
|
|
- await nextTick()
|
|
|
- rect = imgGroup.getClientRect()
|
|
|
- const img = await imgGroup!.toImage({
|
|
|
+ setViewport(rect);
|
|
|
+ await nextTick();
|
|
|
+ rect = imgGroup.getClientRect();
|
|
|
+ const img = (await imgGroup!.toImage({
|
|
|
pixelRatio: 1,
|
|
|
quality: 1,
|
|
|
- mimeType: 'image/png'
|
|
|
- }) as HTMLImageElement
|
|
|
- const start = invMat.value.point({x: rect.x, y: rect.y + rect.height})
|
|
|
- const end = invMat.value.point({x: rect.x + rect.width, y: rect.y})
|
|
|
- const name = onlyId().replace(/\-/g, '')
|
|
|
- const path = name + '.png'
|
|
|
+ mimeType: "image/png",
|
|
|
+ })) as HTMLImageElement;
|
|
|
+ const start = invMat.value.point({ x: rect.x, y: rect.y + rect.height });
|
|
|
+ const end = invMat.value.point({ x: rect.x + rect.width, y: rect.y });
|
|
|
+ const name = onlyId().replace(/\-/g, "");
|
|
|
+ const path = name + ".png";
|
|
|
|
|
|
const image = writer.addImage(
|
|
|
path,
|
|
@@ -184,23 +195,23 @@ export const useGetDXF = () => {
|
|
|
1,
|
|
|
0
|
|
|
);
|
|
|
- image.ratio = Math.abs(end.x - start.x) / img.width
|
|
|
+ image.ratio = Math.abs(end.x - start.x) / img.width;
|
|
|
genPromises.push(
|
|
|
fetch(img.src)
|
|
|
.then((res) => res.blob())
|
|
|
- .then((blob) => zip.file(path, blob, { }))
|
|
|
+ .then((blob) => zip.file(path, blob, {}))
|
|
|
);
|
|
|
viewer.setViewMat(oldViewMat);
|
|
|
};
|
|
|
|
|
|
for (const _item of store.sortItems) {
|
|
|
if (_item.hide) continue;
|
|
|
-
|
|
|
+
|
|
|
const type = store.getType(_item.id);
|
|
|
let item;
|
|
|
let mat;
|
|
|
switch (type) {
|
|
|
- case 'sequentLine':
|
|
|
+ case "sequentLine":
|
|
|
item = _item as SLineData;
|
|
|
writer.addLWPolyline(
|
|
|
item.points.map((p) => ({ point: point2d(p.x, -p.y) })),
|
|
@@ -211,11 +222,11 @@ export const useGetDXF = () => {
|
|
|
}
|
|
|
);
|
|
|
break;
|
|
|
- case 'line':
|
|
|
+ case "line":
|
|
|
const litem = _item as LineData;
|
|
|
- litem.lines.forEach(line => {
|
|
|
- const a = litem.points.find(p => p.id === line.a)!
|
|
|
- const b = litem.points.find(p => p.id === line.b)!
|
|
|
+ litem.lines.forEach((line) => {
|
|
|
+ const a = litem.points.find((p) => p.id === line.a)!;
|
|
|
+ const b = litem.points.find((p) => p.id === line.b)!;
|
|
|
// writer.addLine(
|
|
|
// point3d(a.x, a.y, 0),
|
|
|
// point3d(b.x, b.y, 0),
|
|
@@ -237,7 +248,7 @@ export const useGetDXF = () => {
|
|
|
).toString(),
|
|
|
}
|
|
|
);
|
|
|
- })
|
|
|
+ });
|
|
|
|
|
|
break;
|
|
|
|
|
@@ -269,14 +280,14 @@ export const useGetDXF = () => {
|
|
|
|
|
|
case "arrow":
|
|
|
item = _item as ArrowData;
|
|
|
- writer.addLWPolyline(
|
|
|
- item.points.map((p) => ({ point: point2d(p.x, -p.y) })),
|
|
|
- {
|
|
|
- flags: LWPolylineFlags.None,
|
|
|
- constantWidth: item.strokeWidth,
|
|
|
- trueColor: TrueColor.fromHex(item.fill || "#FFFFFF").toString(),
|
|
|
- }
|
|
|
- );
|
|
|
+ // writer.addLWPolyline(
|
|
|
+ // item.points.map((p) => ({ point: point2d(p.x, -p.y) })),
|
|
|
+ // {
|
|
|
+ // flags: LWPolylineFlags.None,
|
|
|
+ // constantWidth: item.strokeWidth,
|
|
|
+ // trueColor: TrueColor.fromHex(item.fill || "#FFFFFF").toString(),
|
|
|
+ // }
|
|
|
+ // );
|
|
|
const isEnd = [PointerPosition.end, PointerPosition.all].includes(
|
|
|
item.pointerPosition || PointerPosition.start
|
|
|
);
|
|
@@ -286,6 +297,7 @@ export const useGetDXF = () => {
|
|
|
|
|
|
for (let i = 0; i < item.points.length - 1; i++) {
|
|
|
const line = [item.points[i], item.points[i + 1]];
|
|
|
+ const nline = [...line];
|
|
|
const vector = lineVector(line);
|
|
|
|
|
|
if (isStart) {
|
|
@@ -293,6 +305,7 @@ export const useGetDXF = () => {
|
|
|
.clone()
|
|
|
.multiplyScalar(item.pointerLength!)
|
|
|
.add(line[0]);
|
|
|
+ nline[0] = start;
|
|
|
const l1 = verticalVectorLine(
|
|
|
vector,
|
|
|
start,
|
|
@@ -314,6 +327,7 @@ export const useGetDXF = () => {
|
|
|
.clone()
|
|
|
.multiplyScalar(-item.pointerLength!)
|
|
|
.add(line[1]);
|
|
|
+ nline[1] = start;
|
|
|
const l1 = verticalVectorLine(
|
|
|
vector,
|
|
|
start,
|
|
@@ -330,9 +344,17 @@ export const useGetDXF = () => {
|
|
|
stroke: item.fill,
|
|
|
});
|
|
|
}
|
|
|
+ writer.addLWPolyline(
|
|
|
+ nline.map((p) => ({ point: point2d(p.x, -p.y) })),
|
|
|
+ {
|
|
|
+ flags: LWPolylineFlags.None,
|
|
|
+ constantWidth: item.strokeWidth,
|
|
|
+ trueColor: TrueColor.fromHex(item.fill || "#FFFFFF").toString(),
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
case "image":
|
|
|
item = _item as ImageData;
|
|
|
await writeImage($stage.findOne<Group>(`#${item.id}`)!, item);
|
|
@@ -349,7 +371,7 @@ export const useGetDXF = () => {
|
|
|
}
|
|
|
|
|
|
let dxfString = writer.stringify();
|
|
|
- zip.file( onlyId() + ".dxf", dxfString);
|
|
|
+ zip.file(onlyId() + ".dxf", dxfString);
|
|
|
return Promise.all(genPromises).then(() =>
|
|
|
zip.generateAsync({ type: "blob" })
|
|
|
);
|