我是靠谱客的博主 贤惠含羞草,这篇文章主要介绍python 连接CAD 修改图斑点顺序到西北角,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
1.通过点到矩形外框西北角的距离,以及方位角的大小来判断西北角的位置。不是很理想。 2.通过面积判断结果是否正确,7000个点测试时,图斑生成正常。 希望程序对测绘工作有些帮助。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import win32com.client import pythoncom import tkinter as tk from tkinter import messagebox import numpy import math def vtpnt(x, y, z=0): """坐标点转化为浮点数""" return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z)) def vtobj(obj): """转化为对象数组""" return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj) def vtFloat(list): """列表转化为浮点数""" return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list) def vtInt(list): """列表转化为整数""" return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list) def vtVariant(list): """列表转化为变体""" return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list) def list_format_conversion(old_list, leixing=False, step=2, deduction=None): if leixing is False: leixing = list else: leixing = tuple new_list = [] for counte in range(0, len(old_list), step): new_list.append(leixing(old_list[counte:counte + step])[:deduction]) return new_list ''' 进位,返回str ''' def rrd(point, f): if f == 0: point = str(int(point)) elif f > 0: SP = str(numpy.around(point, f)) sy = SP.split(".") n = len(sy[1]) if n < f: sj = f - n point = sy[0] + "." + sy[1] + "0" * sj else: point = SP elif f < 0: tk.messagebox.showinfo('提示', "取位不可为负") return point ''' 计算顺时针或逆时针 green方法判断面积正负 ''' def polyline_order(P): n = len(P) P.append(P[0]) S = 0 for i in range(0, n): S = S + (P[i][0] + P[i + 1][0]) * (P[i + 1][1] - P[i][1]) if S > 0: return False # 逆时针 else: return True # 顺时针 ''' 计算方位角 ''' def azimuth(x1, y1, x2, y2): angle = 0 dx = x2 - x1 dy = y2 - y1 if dx == 0 and dy > 0: # 判断象限 angle = 0 if dx == 0 and dy < 0: angle = 180 if dy == 0 and dx > 0: angle = 90 if dy == 0 and dx < 0: angle = 270 if dx > 0 and dy > 0: angle = math.atan(dx / dy) * 180 / math.pi elif dx < 0 and dy > 0: angle = 360 + math.atan(dx / dy) * 180 / math.pi elif dx < 0 and dy < 0: angle = 180 + math.atan(dx / dy) * 180 / math.pi elif dx > 0 and dy < 0: angle = 180 + math.atan(dx / dy) * 180 / math.pi angle = 90 - angle angle = math.radians(angle) return angle # j ''' 计算西北角 ''' def distance(point, poly): d = {} min_d = [] azimuth_2 = {} for Endpoint in poly: distance_list = math.sqrt((Endpoint[0] - point[0]) ** 2 + (Endpoint[1] - point[1]) ** 2) d.update({tuple(Endpoint): distance_list}) for key, val in d.items(): if val == min(d.values()): min_d.append(key) if len(min_d) > 1: for x, y in min_d: angle = azimuth(x, y, point[0], point[1]) azimuth_2.update({(x, y): angle}) min_point = max(azimuth_2, key=azimuth_2.get) return min_point elif len(min_d) == 1: min_point = min_d[0] return min_point ''' 重画西北角 ''' def Select_element(): global acad, doc, mp, original_area acad = win32com.client.Dispatch("AutoCAD.Application") doc = acad.ActiveDocument doc.Utility.Prompt("n醉后不知天在水n满船清梦压星河n") mp = doc.ModelSpace # 模型空间 tk.messagebox.showinfo('提示', "请在屏幕拾取图元,以Enter键结束") try: doc.SelectionSets.Item("SS1").Delete() except: tk.messagebox.showinfo('警告', "Delete selection failed") original_area = [] slt = doc.SelectionSets.Add("SS1") slt.SelectOnScreen() for x in slt: if x.ObjectName == "AcDbPolyline": original_area.append(x.Area) # 面积 old_coord = list_format_conversion(x.Coordinates, leixing=True) new_coord = tuple({}.fromkeys(old_coord).keys()) # 剔除重复点 list4 = [] for ier in old_coord: if ier not in list4: list4.append(ier) else: center, radius = vtpnt(ier[0], ier[1], 0), 2 cenobj = acad.ActiveDocument.Layers.Add("01重复点") acad.ActiveDocument.ActiveLayer = cenobj ClrNum = 2 cenobj.color = ClrNum mp.AddCircle(center, radius) Polyline_coordinates = [list(x) for x in new_coord] # 改变坐标格式【【1,2】,【4,3】,【6,7】】 if not polyline_order(Polyline_coordinates): # 若逆时针,反转列表 Polyline_coordinates.reverse() poly_x = [x[0] for x in Polyline_coordinates] poly_y = [y[1] for y in Polyline_coordinates] max_y = max(poly_y) min_x = min(poly_x) northwest_point = [min_x, max_y] # 矩形框西北角 northwest_point_end_point_distance = distance(northwest_point, Polyline_coordinates) # 图斑最靠近图框西北角的坐标 count = Polyline_coordinates.index(list(northwest_point_end_point_distance)) # 查找在列表中的位置 Polyline_coordinates = Polyline_coordinates[:-1] for _ in range(0, count): delete = Polyline_coordinates.pop(0) Polyline_coordinates.append(delete) Polyline_coordinates = numpy.array([m for n in Polyline_coordinates for m in n], dtype=numpy.float) points = vtFloat(Polyline_coordinates) # 设定图层 layerobj = acad.ActiveDocument.Layers.Add("02重置图斑") acad.ActiveDocument.ActiveLayer = layerobj # 多段线颜色 ClrNum = 1 layerobj.color = ClrNum polylineobj = mp.AddLightweightPolyline(points) polylineobj.Closed = True def judge(): modify_area = [] try: doc.SelectionSets.Item("SS1").Delete() except: tk.messagebox.showinfo('警告', "Delete selection failed") slt = doc.SelectionSets.Add("SS1") slt.SelectOnScreen() for x in slt: if x.ObjectName == "AcDbPolyline": if x.Layer == "02重置图斑": modify_area.append(x.Area) A = sum(original_area) B = sum(modify_area) C = abs(A - B) if C <= 0.000001: tk.messagebox.showinfo('提示', "成功!") else: tk.messagebox.showinfo('提示', "误差超限!") Select_element() judge()

最后

以上就是贤惠含羞草最近收集整理的关于python 连接CAD 修改图斑点顺序到西北角的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部