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

Manipulate svg #82

Open
KiraL91 opened this issue Nov 2, 2021 · 2 comments
Open

Manipulate svg #82

KiraL91 opened this issue Nov 2, 2021 · 2 comments

Comments

@KiraL91
Copy link

KiraL91 commented Nov 2, 2021

Hello! I find this package very interesting.
I've been testing it and I don't know if there is a simple way to work with the SVG. Specifically, I do not know if it is possible to access the elements of the SVG by layers once it has already been included in the DOM.
I know that the DXF entities are available within the helper.parsed object, however, they are not directly linked to the SVG since the DOM, once the SVG is embedded, hardly contains any information about the original DXF (i.e. layers).
So far I have been able to directly modify the SVG before adding it to the DOM (eg adding ids or custom properties to the elements) but I don't think this is the most efficient approach, mainly because I can only add some simple properties that are not directly linked to the original DXF.

Here is my attempt.

foo(): string {
   const helper = new Helper(dxf);
   let svg = helper.toSVG();
   const parser = new DOMParser();
   const html = parser.parseFromString(svg, 'text/html');
   const svg = html.getElementsByTagName('svg')[0];
   svg.querySelectorAll('g').forEach((g, i) => {
      if(g.hasAttribute('stroke')) g.setAttribute('id', i + '');
    });
   const encoder = new XMLSerializer();
   return encoder.serializeToString(svg);
}

So, to sum up, I'd like to add a property to the elements for filtering them by layer in a later processing step (i.e. displaying elements by layer or hidding them). Any ideas are welcome.

Many thanks in advanced

@bjnortier
Copy link
Collaborator

Hi. There is no support for adding properties to SVG elements during the transformation step, but I can imagine it's useful to embed the layer number in the element for filtering. I don't work on this anymore but can merge a pull request if you create one.

@KiraL91
Copy link
Author

KiraL91 commented Nov 3, 2021

Hello. I made pull request #83 to add the "layer" property to the entities whenever possible. Could you merge it?
Thanks in advance.

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