0

我正在做一个项目来创建一个工具来帮助工程师自动化绘图任务,当我的公司使用 ZWcad 而不是 Autocad 时,我发现自己不得不使用 pyzwcad,但我在一个地方找不到足够的信息,或者我没有搜索在正确的地方,因为我是一名初学者程序员,我需要在一个地方收集我收集的所有数据。

4

2 回答 2

1

首先你需要导入 pyzwacad

from pyzwcad import *

或者只是导入使用的方法

from pyzwcad import ZwCAD, ZCAD, APoint, aDouble

我使用 API 自动执行任务的首选方法是让用户在使用该工具之前启动程序。

所以我们现在需要定义cad对象

acad = ZwCAD()

在接下来的段落中,我将总结我在项目中使用的一些 pyzwacad 方法。

  1. 在当前图形文件中添加线型。

    def G_add_line_typ(ltyp_name, acad):
        line_type_found = False
        for ltyp in acad.doc.Linetypes:
    
            if ltyp.name == ltyp_name:
                line_type_found = True
        if not line_type_found:
            acad.doc.Linetypes.Load(ltyp_name, "ZWCADiso.lin")
    
  2. 画正方形:

    X_coo:为正方形中心点的 X 坐标。

    y_coo:是正方形中心点的 Y 坐标。

    我们需要为方形点坐标创建一个数组/列表,例如第一个点将占据列表中的前两个位置,所以

    list[0] 是第一个点 X 坐标和

    列表1是第一个点 Y 坐标。

    def draw_sqr(acad, X_coo, y_coo, d, w, n_color):
        sqr_pts = [X_coo - (d / 2), y_coo + w / 2, X_coo - (d / 2), y_coo - w / 2, X_coo + (d / 2), y_coo - w / 2, X_coo + (d / 2), y_coo + w / 2, X_coo - (d / 2), y_coo + w / 2]
        sqr = acad.model.AddLightWeightPolyline(aDouble(sqr_pts))
        sqr.color = n_color
        # shape color is an integer from color index 1 for red.
    

在此处输入图像描述

于 2021-10-21T18:16:44.933 回答
0

3-添加圈子:

# add insertion point
x_coo = 50
y_coo = 50
c1 = APoint(x_coo, y_co)
radius = 500
circle= acad.model.AddCircle(c1, radius)

4-旋转对象:

# add base point
x_coo = 50
y_coo = 50
base_point= APoint(x_coo, y_co)
r_ang = (45 * np.pi) / 180
object.Rotate(base_point, r_ang)
# object is refering to the object name it could be difrent based on your code

5-添加文字:

# add insertion point
x_coo = 50
y_coo = 50
pttxt = APoint(x_coo, y_coo)
txt_height = 200 # text height
text = acad.model.AddText("text string", pttxt, txt_height)

6-更改文本对齐方式:

# first we need to sort the current insertion point for the exist text object as it will be reset after changing the alignment.

old_insertion_point = APoint(text.InsertionPoint)
# text is refering to text object name it could be difrent based on your code


text.Alignment = ZCAD.zcAlignmentBottomCenter

# modify the text insertion point as the above step automaticaly reset it to (0, 0)
text.TextAlignmentPoint = old_insertion_point

7-添加旋转尺寸线:

我们需要定义 3 个点

起点,终点和尺寸文本点,我们应该使用数学库来使用弧度方法

import math
acad = ZwCAD()
st_dim = APoint(0, 0)

end_dim = APoint(100, 30)

text_dim = APoint(50, 15)

dim_line = acad.model.AddDimRotated(st_dim, end_dim, text_dim, math.radians(30))

acad.Application.ZoomAll()

上面的代码可用于通过在水平尺寸的角度位置使用 0 或对于垂直尺寸使用 math.radians(90) 来添加衬里尺寸线。

8-添加对齐尺寸线:

与上面相同,但不使用旋转角度。

acad = ZwCAD()
st_dim = APoint(0, 0)

end_dim = APoint(100, 30)

text_dim = APoint(50, 15)

dim_line = acad.model.AddDIMALIGNED(st_dim, end_dim, text_dim)

9-覆盖现有尺寸线文本:

dim_line.TextOverride = "new text"

“dim_line”是指想要的尺寸线对象名称。

于 2021-10-22T12:44:21.193 回答