I have a problem with getting entities which remain inside viewports in paperspace.
p1 point is left bottom corner, p2 point is right top corner. When I plot line from p1 to p2 with the following code I get the following result as a picture of Autocad screen which seems there is an offset along x and y axis. Can anyone help me?
for vp in layout.viewports():
xm, ym, zm = vp.dxf.view_center_point
center = vp.dxf.center
w = int(vp.dxf.width)
h = int(vp.dxf.height)
h2 = int(vp.dxf.view_height)
w2 = h2/h*w
p1 = xm - w2/2, ym - h2/2
p2 = xm + w2/2, ym + h2/2
msp.add_line(start=p1, end=p2, dxfattribs={'layer': 'dijar'})
enter image description here
autocad screen
I have a problem with getting entities which remain inside viewports in paperspace.
p1 point is left bottom corner, p2 point is right top corner. When I plot line from p1 to p2 with the following code I get the following result as a picture of Autocad screen which seems there is an offset along x and y axis. Can anyone help me?
for vp in layout.viewports():
xm, ym, zm = vp.dxf.view_center_point
center = vp.dxf.center
w = int(vp.dxf.width)
h = int(vp.dxf.height)
h2 = int(vp.dxf.view_height)
w2 = h2/h*w
p1 = xm - w2/2, ym - h2/2
p2 = xm + w2/2, ym + h2/2
msp.add_line(start=p1, end=p2, dxfattribs={'layer': 'dijar'})
enter image description here
autocad screen
1 Answer
Reset to default 0Important: the first VIEWPORT entity is a special entity that defines the view position of the paperspace layout in the CAD application.
The following code shows how to add DXF entities to the modelspace at the location of the first real VIEWPORT entity:
import ezdxf
def main():
doc = ezdxf.readfile("Layout1.dxf")
msp = doc.modelspace()
layout1 = doc.paperspace("Layout1")
# first VIEWPORT is a special entity
viewport = layout1.viewports()[1]
# transformation matrix from modelspace to paperspace
m = viewport.get_transformation_matrix()
# transformation matrix from paperspace to modelspace
m.inverse()
# get corner vertices of the viewport in paperspace coordinates
p0, p1 = viewport.clipping_rect()
# get corner vertices of the viewport in modelspace coordinates
mp0 = m.transform(p0)
mp1 = m.transform(p1)
msp.add_line(mp0, mp1, dxfattribs={"color": 1})
doc.saveas("Layout1_extended.dxf")
if __name__ == "__main__":
main()
content of Layout1.dxf
and the corresponding modelspace
content of the create Layout1_extended.dxf
and the corresponding modelspace