-
I do dxf file to png file, and now I want to kown how many pixels in png file 1 millimeter in dxf file is equal to? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
My code is same with [(https://github.com//issues/226)]. |
Beta Was this translation helpful? Give feedback.
-
fundamentally, you need the transform between the CAD coordinate system and the image coordinate system. Then, assuming the cad coordinates are in millimetres anyway, any distance in the CAD can be converted to a distance in pixels and vice versa. If the CAD isn't in millimetres, then you need to first scale between the CAD units and millimetres. see my answer here: #219 (comment) for one possible solution for getting the transformation between the cad coordinate system and pixels of the resulting image. def transform_vector(x:float, y: float, m: np.ndarray) -> Tuple[float, float]:
return tuple((m @ [x, y, 0])[:2].tolist()) @mozman, since multiple people have found the need to obtain this transformation, do you think some facility for this could be added to ezdxf directly? When I wrote the solution to #219 I used numpy arrays rather than Matrix44 since I needed a homogeneous matrix to transform 2D points (i.e. 3x3 matrix). I suppose a Matrix44 could be used which (orthographically) projects 3D points down to z=0 for the image coordinate system. What do you think? |
Beta Was this translation helpful? Give feedback.
-
also, if you just want a single scale factor, it should be sufficient in your case to take the ratio between the x axis limits (i.e. the data width) and the image width. So x1, x2 = ax.get_xlim()
width_mm = x2 - x1 # assuming cad coordinates are in mm
width_px = # <obtain image width>
mm_to_px = width_px / width_mm
length_mm = 10
length_px = length_mm * mm_to_px |
Beta Was this translation helpful? Give feedback.
-
DXF and measurement units are not a good match, there is practically no documentation, therefore I don't want to open this can of worms ;-). But this is good topic for a How-To, I do the "restructured text" formatting if you provide the content. |
Beta Was this translation helpful? Give feedback.
-
I do test on 284 files and results show me that pix/mm values are right |
Beta Was this translation helpful? Give feedback.
-
Super thanks mbway |
Beta Was this translation helpful? Give feedback.
-
when coordinate of entities in dxf , i found some lines offset a little to pixel on png. |
Beta Was this translation helpful? Give feedback.
-
@Aidmx are you saying that simply using the scale factor to get the transformation between CAD and the png results in a slight error in the resulting pixel coordinates? If so then I also encountered this. I think it's to do with the coordinate systems being flipped relative to one another. The CAD coordinate system has +y pointing up, and images have +y pointing down. This also means that a pixel coordinate refers to the top left of a pixel whereas simply scaling a CAD coordinate to the image size will be addressing the bottom left of a pixel. I think this is where the small error comes from. My solution here: #219 (comment) adds 1 to the coordinates before flipping and gave more accurate results for me, but the algorithm may not be perfect. |
Beta Was this translation helpful? Give feedback.
fundamentally, you need the transform between the CAD coordinate system and the image coordinate system. Then, assuming the cad coordinates are in millimetres anyway, any distance in the CAD can be converted to a distance in pixels and vice versa. If the CAD isn't in millimetres, then you need to first scale between the CAD units and millimetres.
see my answer here: #219 (comment) for one possible solution for getting the transformation between the cad coordinate system and pixels of the resulting image.
note that to transform a vector (i.e. distance) rather than a point using a homogeneous transformation matrix can be done with: