最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Getting entities of viewports - Stack Overflow

programmeradmin2浏览0评论

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

Share Improve this question asked 8 hours ago Bilal GüngörBilal Güngör 1 New contributor Bilal Güngör is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

Important: 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

发布评论

评论列表(0)

  1. 暂无评论