Reputation: 2363
I am using the Edit2D extension on an svf created from a 2D dwg file and have a question about transforms. The Autodesk.Edit2D.Polygon
's that are created have a getArea()
method which is great. However it's not in the correct unit scale. I tested one and something that should be roughly 230sf in size is coming back as about 2.8.
I notice that the method takes an argument of type Autodesk.Edit2D.MeasureTransform
which I'm sure is what I need, however I don't know how to get that transform. I see that I can get viewer.model.getData().viewports[1].transform
. However, that is just an array of 16 numbers and not a transform object so it creates an error when I try to pass it in.
I have not been able to find any documentation on this. Can someone tell me what units this is coming back in and/or how to convert to the same units as the underlying dwg file?
Related question, how do I tell what units the underlying DWG is in?
EDIT
To add to this, I tried to get all polylines in the drawing which have an area property. In this case I was able to figure out that the polyline in the underlying dwg was reporting its area in square inches (not sure if that's always the case). I generated Edit2D polygons based on the polylines so it basically just drew over them.
I then compared the area property from the polyline to the result of getArea()
on the polygon to find the ratio. In this case it was always about 83 or 84 times smaller than the square foot value of the polyline it came from (there is some degree of error in my tracing system so I don't expect they would be exact at this point). However, that doesn't fit any unit value that I know of. So remaining questions:
Upvotes: 0
Views: 283
Reputation: 640
I know this is a little late, but I wanted to share my findings for posterity, even though you have already answered your own question. This expands on it for related use cases, particularly around manipulating the number to display the results. I am using the Autodesk.Edit2D extension which enables this methodology, and I'm getting the total area of shapes in myShapes array.
const edit2d = myViewerVariable.getExtension('Autodesk.Edit2D');
const unitHandler = edit2d.defaultContext.unitHandler;
const transform = unitHandler.measureTransform;
const totalArea = myShapes.reduce(
(internalAccumulator, internalValue) => {
{
return internalAccumulator + internalValue?.getArea(transform);
}
},
0.0
);
At this point, my totalArea
could be for example, 280480.0264359908
. With no unit of measurement. The unitHandler variable has a config object key (unitHandler.config) that contains both the displayUnits (set via the Settings in Viewer) and the layerUnits (the default units that the layer is defined in the AutoCAD) in case you want to know what you're working with at this point. At this point, I now know that the number above is in inches.
You can then use the unitHandler
object to convert the specified area to a user-friendly string, which is output with the selected displayUnits with areaToString
.
unitHandler.areaToString(totalArea)
returns 180 m² 9545 cm²
if my unit of measurement is set to "Meters and centimeters" in the viewer settings. Likewise, the same function will return 1947'² 112"²
if "Feet and decimal inches" is selected in the viewer settings.
Upvotes: 0
Reputation: 7070
Maybe you missed the section 3.2 Units for Areas and Lengths
of https://forge.autodesk.com/en/docs/viewer/v7/developers_guide/advanced_options/edit2d-use/
If you use Edit2D without the MeasureExtension, it will display all coordinates in model units. You can customize units by modifying or replacing DefaultUnitHandler. More information is available in the Customize Edit2D tutorial.
and https://forge.autodesk.com/en/docs/viewer/v7/developers_guide/advanced_options/edit2d-customize/
BTW, we can get the DefaultUnitHandler
by edit2dExt.defaultContext.unitHandler
Upvotes: 1
Reputation: 2363
Ok after a great deal of experimentation and frustration I think I have it working. I ended up looking direction into the js for the getArea()
method in dev tools. Searching through the script, I found a class called DefaultMeasureTransform
that inherits from MeasureTransform
and takes a viewer argument. I was able to construct that and then pass it in as an argument to getArea()
:
const transform = new Autodesk.Edit2D.DefaultMeasureTransform(viewer);
const area = polygon.getArea(transform);
Now the area
variable matches the units in the original cad file (within acceptable rounding error anyway, it's like .05 square inches off).
Would be nice to have better documentation on the coordinate systems, am I missing it somewhere? Either way this is working so hopefully it helps someone else.
Upvotes: 1