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

Polygon Example & Support #183

Open
traik06 opened this issue Nov 20, 2024 · 4 comments
Open

Polygon Example & Support #183

traik06 opened this issue Nov 20, 2024 · 4 comments

Comments

@traik06
Copy link

traik06 commented Nov 20, 2024

Hello,

Is there an example of create a .dxf of multiple polygons? I cant seem to find it in the documentation on how to implement it.

Please bear with me here I am new to creations of .dxf files

Here is an example of the code I am trying to use as a tester

These would be lat and lng coordinates

const coordinates = [
   // polygon number 1
   [
   [0, 0, 0], // Vertex 1: x, y, z
   [0, 10, 0], // Vertex 2: x, y, z
   [10, 10, 0],// Vertex 3: x, y, z
   [10, 0, 0], // Vertex 4: x, y, z
   [0, 0, 0], // Closing vertex to complete the polygon
   ],
   // polygon number 2
   [
   [5, 0, 0], // Vertex 1: x, y, z
   [55, 10, 0], // Vertex 2: x, y, z
   [67, 10, 6],// Vertex 3: x, y, z
   [87, 0, 8], // Vertex 4: x, y, z
   [5, 0, 0] // Closing vertex to complete the polygon
   ],
];

Thanks very much

@tarikjabiri
Copy link
Member

Hi

You can use the Polyline 3D

import {
  DxfWriter,
  PolylineFlags,
  type PolylineVertex,
} from "@tarikjabiri/dxf";
import { writeFileSync } from "node:fs";

const coordinates = [
  // polygon number 1
  [
    [0, 0, 0], // Vertex 1: x, y, z
    [0, 10, 0], // Vertex 2: x, y, z
    [10, 10, 0], // Vertex 3: x, y, z
    [10, 0, 0], // Vertex 4: x, y, z
  ],
  // polygon number 2
  [
    [5, 0, 0], // Vertex 1: x, y, z
    [55, 10, 0], // Vertex 2: x, y, z
    [67, 10, 6], // Vertex 3: x, y, z
    [87, 0, 8], // Vertex 4: x, y, z
  ],
];

// No need to duplicate the first vertex to close the polygon

const dxf = new DxfWriter();
const pvs = coordinates.map((vertices) => {
  return vertices.map((v) => {
    const pv: PolylineVertex = {
      point: {
        x: v[0],
        y: v[1],
        z: v[2],
      },
    };
    return pv;
  });
});
for (const pv of pvs) {
  dxf.addPolyline3D(pv, {
    flags: PolylineFlags.Closed,
  });
}

writeFileSync("polygons.dxf", dxf.stringify(), {
  encoding: "utf-8",
});

image

@tarikjabiri
Copy link
Member

These would be lat and lng coordinates

Important

There is no support for geographic coordinates, you need to use projected coordinates using some transformation.
DXF support only cartesien coordinates

@traik06
Copy link
Author

traik06 commented Nov 20, 2024

@tarikjabiri

Do you have any recommendations on a good approach?

So what I would need to do is convert these coordinates against something like an EPSG code?

then once they are transformed I should just be able to import it into CAD and view it in the region that I have set for the projection?

@tarikjabiri
Copy link
Member

proj4js can do transformations, you can use https://epsg.io/ to get the the code for proj4js, you can search for you projection there and select proj4js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants