File Import of .svg file gets scale wrong by a factor of about 7.6

I have an svg file LegBracket2
(generated by Shaper Trace), which has the first few lines saying:

svg xmlns=“SVG namespace” version=“1.0” viewBox=“275.5789489746094 598.451416015625 765.0440673828125 761.4630126953125” width=“101.20895474751791mm” height=“100.73521105448405mm”>.centerline { stroke: red; fill: none; stroke-width: 1px; }

<path id=“path-143” class=“centerline” d="M938.5535977729901 1351.0039487914662 C936.3987857

Note that the height and width are about 100 mm.

Using the Rhino File>>Import menu item and importing the .svg file results in a correct import except that the object is scaled to being about 7.6 times too large:
LegBracket2.3dm (2.4 MB)

I think that this is a bug, but who knows? I was using the Shaper Trace because it scans quickly and keeps 1:1 scaling information, but if Rhino gets it wrong, this is not much help. Inkscape seems to manage it better. Sigh.

Bob

Edit: The .svg file seems to get interpretted by the forum, rather than included. Let me know if you need the actual file and I can try to provide it some other way. Sorry!

Does Shape Tracer have any other export formats? DWG for example. That might work better for now. We can take a look at the SVG Import as well of course.

I believe that Shaper Trace only does .svgs, since that is what their main carving product consumes. I was using Rhino in a very minor way, just to clean up the mess and to create a .dxf file, which I then pass on to CamBam to create g-code for the CNC.

I hope that the file above is sufficient for your purposes. I had expected the Import file to provide a link to the .svg, not display it inline, and I am not sure how to provide it otherwise.

Thanks for taking a look at this.

Bob

Hi Bob -

That is the size of the viewport, not of the geometry that is seen through the viewport. The viewBox information that is stated before that size tells a browser which part and at which scale the geometry will be rendered at.

When you open that file in Inkscape and check the document properties, you will see this:
image

Inkscape is scaling the geometry by 0.132292.

If you look at two of the longest curves in the SVG code and check their coordinates, you will see that these match with what Rhino is drawing.

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="275.5789489746094 598.451416015625 765.0440673828125 761.4630126953125" width="101.20895474751791mm" height="100.73521105448405mm">
  <style id="svg-style">.centerline { stroke: green; fill: none; stroke-width: 3px; }
</style>
  <path id="path-145" class="centerline" d="M894 1082 C918 1082 942 1082 966 1081 C967 1081 969 1080 971 1080 C977 1080 984 1080 991 1080 C994 1080 998 1080 1001 1081 C1001 1081 1002 1082 1002 1082 C1003 1083 1004 1083 1006 1083 C1006 1083 1007 1082 1008 1083 C1009 1083 1009 1084 1010 1085 C1013 1086 1015 1086 1018 1087 C1020 1088 1022 1090 1023 1091 C1026 1094 1029 1096 1030 1100 C1033 1106 1035 1112 1036 1119 C1037 1123 1037 1128 1037 1132 C1037 1158 1036 1184 1036 1210 C1036 1241 1037 1272 1037 1303 C1037 1307 1037 1312 1036 1316 C1036 1320 1035 1324 1033 1328 C1030 1333 1026 1338 1022 1342 C1020 1344 1019 1346 1017 1347 C1010 1349 1003 1351 997 1353 C994 1353 992 1353 989 1353 C974 1353 959 1353 944 1353 C928 1353 912 1352 897 1352 C858 1352 819 1352 781 1352 C764 1352 747 1350 731 1350 C718 1350 704 1351 691 1351 C684 1351 676 1350 668 1350 C663 1350 658 1350 653 1350 C651 1350 649 1349 647 1349 C640 1349 633 1349 627 1349 C625 1349 624 1350 622 1351"/>
  <path id="path-152" class="centerline" d="M297 608 C300 607 303 605 306 604 C309 603 313 601 317 601 C325 600 332 599 340 599 C373 599 407 600 440 599 C457 599 473 598 490 598 C496 598 502 598 508 599 C511 599 513 599 516 600 C519 601 522 602 526 604 C532 607 537 611 543 615 C544 616 544 617 545 618 C547 622 549 626 549 630 C551 637 551 644 551 651 C552 782 552 913 553 1045 C553 1048 553 1051 553 1053 C554 1056 554 1058 555 1059 C557 1065 565 1070 563 1076 C561 1080 555 1071 552 1068 C550 1065 550 1062 549 1059 C548 1055 548 1051 548 1047 C547 914 545 781 546 649 C546 645 549 634 551 638 C553 643 550 649 549 655"/>
</svg>

-wim

I see that scaling factor. And the reciprocal of 0.132292 is about 7.56, which is about the extent of the scaling problem that I am getting. Interesting!

Clearly I am not interested in viewport size, but in the dimensions of the underlying object. The actual bracket was 100mm each way, and my tracing is fractionally larger. I need to get this dimension into the .dxf file for carving.

When I save the file from Inkscape as a .dxf, I get:
LegBracket2_Inkscape.dxf (82.1 KB)

which opens up in CamBam with a dimension of 100mm each way.

When I export the file from Rhino as a .dxf, I get:
LegBracket2_Rhino.dxf (312.9 KB)

which opens up in CamBam 7.6 times too big.

Looking back at the .svg file, the first line specifies the viewBox parameter as
viewBox=“275.5789489746094 598.451416015625 765.0440673828125 761.4630126953125”
and the width and height as width=“101.20895474751791mm” height=“100.73521105448405mm”.

The third and fourth viewBox parameters are the width and height of the viewport, while the ‘width’ and ‘height’ attributes are the dimensions of the image to be displayed in the viewport.

I believe that the Inkscape scaling factor (0.132292) is to show that the image has had to be scaled by this amount to fit in the Inkscape viewport.

Rhino, on the other hand, has scaled the image at Import time to fit the given viewport dimensions and has used those coordinates for the object itself. Thus when it Exports the result as a .dxf, it is off by the ratio of the original width (101.2) and the viewport width (765.0).

The svg attributes are reasonably well documented at viewBox - SVG: Scalable Vector Graphics | MDN.

I continue to believe that this is a problem with Rhino’s svg Import command.

Hi Bob -

No.
The width and heigth attributes specify the viewport. Not the attributes of the viewBox. That specifies what is seen and at what scale.

… but that’s already scaled by Inkscape.

You need to look at the coordinates of geometry in the SVG file.
As far as I can tell, Rhino doesn’t scale those.
If those coordinates are wrong, it’s the originating application that makes it so.
-wim

Before we go too deep into this, I just want to acknowledge how weird, complex, and indecipherable the SVG spec is. If you’ve ever tried writing an SVG parser. It’s a nightmare and there is no one true correct rendering for an SVG (when you get deep into it).

That being said, I spent a good while in the spec, and decided an example is probably best to help me grok this, and help others.


The SVG is 101mm wide and 100mm high. Which tracks with this.

The contents of the SVG should scale to the width/height. Here are two examples, one is 101mm x 100mm and one is 202mm x 200mm. If we paste into https://www.svgviewer.dev/ we’ll see they show basically the same SVG. The difference being one displays bigger, even though the contents of the view box and svg are identical.

101mm x 100mm

<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="275 596 765 761" width="101mm" height="100mm">
  <line class="centerline" x1="275" y1="596" x2="765" y2="761" stroke="red" stroke-width="4" />
  <line class="centerline" x1="275" y1="596" x2="1040" y2="1357" stroke="blue" stroke-width="4" />
</svg>

202mm x 200mm

<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="275 596 765 761" width="202mm" height="200mm">
  <line class="centerline" x1="275" y1="596" x2="765" y2="761" stroke="red" stroke-width="4" />
  <line class="centerline" x1="275" y1="596" x2="1040" y2="1357" stroke="blue" stroke-width="4" />
</svg>

If we change the view box, we see the contents gets smaller, because the contents is created relative to the view box.

101mm x 100mm with a 200x200 view box

<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 200 200" width="101mm" height="100mm">
  <line class="centerline" x1="50" y1="50" x2="150" y2="150" stroke="red" stroke-width="4" />
</svg>

101mm x 100mm with a 400 x 400 view box

<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 400 400" width="101mm" height="100mm">
  <line class="centerline" x1="50" y1="50" x2="150" y2="150" stroke="red" stroke-width="4" />
</svg>

My Conclusion

:white_check_mark: The SVG Importer does read the mm width/height and keeps them into account, it will always be the same size regardless of the current documents units.

:negative_squared_cross_mark: The SVG Importer should scale the internal contents of the SVG to the width/height so that the L shape has a bounding box of 101mm x 100mm

I’ve filed this bug for you @bob.h.mackay.
https://mcneel.myjetbrains.com/youtrack/issue/RH-84096/SVG-Importer-does-not-scale-internals-relative-to-the-viewBox-and-Width-Height

Apologies for the issues, for now, I trust you are able to scale manually (as you know the real sizes) and continue with your work?

Thank you for looking into this.

In Inkscape, I see a checkbox that pops up during the Save As process that says “Use Document Unit as base unit”. If I uncheck this, then Inkscape also scales up the resulting .dxf file. I think that this option is probably what you are saying is similar for Rhino, although the issue is certainly at Import time, not at Export time.

Using Rhino as a .svg to .dxf converter is of course a complete abuse of the tool! I mostly use Rhino for Grasshopper, but it was to hand for this conversion.

To the extent that I can use Inkscape, I am fine for what I am doing. Inkscape on the Mac is barely operational, but does at least do this. Shaper Trace is also a pretty crude instrument, but is handy for importing arbitrary artwork, and it really does allow a phone to capture an image with correct scaling and with the correction of parallax effects.

Cheers, Bob

1 Like