#!uer/bin/env python # -*- coding: utf8 -*- import csv import datetime import time import pythoncom import win32com.client import win32com.client as win32 from comtypes.gen._1B47FC7F_AFF7_44A9_88BE_873CC5B70A6E_0_1_0 import acExtendNone class Base(): def connect_cad(self): # connect cad wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument return doc def send_cad(self,str="Please select polyline, right click to end\\ "): ''' :param str: the content to be printed in cad :return: ''' wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument doc. Utility. Prompt(str) def selectObject(self): # Selection set, returns all objects selected wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument try: doc.SelectionSets.Item("SS1").Delete() except: print("Delete selection failed") slt = doc. SelectionSets. Add("SS1") doc.Utility.Prompt("Please select a polyline, right click to end\\ ") slt. SelectOnScreen() return slt # data conversion def vtpnt(self,x,y,z=0): """The coordinate point is converted into a floating point number""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z)) def vtpnt_tuple(self,xyz=(0,0,0)): """The coordinate point is converted into a floating point number""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (xyz)) def vtpnt_tuple1(self,x=0): """The coordinate point is converted into a floating point number""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x)) def vtobj(self, obj): """ into object array""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj) def vtlstf(self, lst): """The list is converted to a floating point number""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, lst) def vtlInt(self, lst): """ list converted to integer """ return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, lst) def vtlstvariant(self,lst): """list converted to variants""" return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, lst) def line_start_end(self, slt): ''' @funtion: Select the line and return the coordinates of the starting point and end point of the line :return: Return the starting point and end point coordinates of the line ''' start_line_point = [] end_line_point = [] if slt. Count == 0: print("No object selected") else: entity = slt[0] name = entity. EntityName for i in slt: if name == "AcDbLine": coor_lst1 = list(i.StartPoint) # coor_lst1.insert(0, 'Straight line (end point)') coor_lst2 = list(i.EndPoint) # coor_lst2.insert(0, '') start_line_point.append(coor_lst1) end_line_point.append(coor_lst2) return start_line_point, end_line_point def point_intersection(self, slt): ''' @funtion: Get the intersection point of the selection set object and return the intersection point coordinates @param slt: slt, select all objects @return: Returns the coordinates of the intersection point ''' # wincad = win32com.client.Dispatch("AutoCAD.Application") # doc = wincad.ActiveDocument # try: # doc.SelectionSets.Item("SS1").Delete() #except: # print("Delete selection failed") # slt = doc. SelectionSets. Add("SS1") # doc.Utility.Prompt("Please select polyline, right click to end\\ ") # slt. SelectOnScreen() retval = [] if slt. Count == 0: print("No object selected") else: print("%s objects have been selected"%slt.Count) entity = slt[0] name = entity. EntityName for i in range(slt. Count): print("Get the intersection point for the %sth time" % (i + 1)) for j in range(1, slt. Count): entity = slt[i] name1 = entity. EntityName entity1 = slt[j] name2 = entity1. EntityName # print(entity,entity1) RetVal = entity. IntersectWith(entity1, acExtendNone) if RetVal == (): print("The current object %s and %s have no intersection:" % (name1, name2)) else: print("The current object %s and %s intersect at: %s" %(name, name1,RetVal)) if RetVal not in retval: retval.append(RetVal) # print(retval) return retval def send_command(self, str): ''' @funtion: send commands to cad @param str: Default command: Trim + Enter + Enter, SendCommand("trim" + chr(13) + chr(13)) chr(13) ASCII carriage return key encoding @return: ''' wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument doc. SendCommand(str) def send_command_trim(self,point,name="dd"): # break line intersection wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument for i in range(len(point)): print("Interrupt %s article"%(i + 1)) doc.SendCommand(name + chr(13) + point[i] + chr(13)) time. sleep(0.5) def tuple_to_list(self, point_list, num=1): ''' @funtion: convert the obtained coordinates into a list, and delete the Z-axis coordinates :param point_list: :return: Returns the x and y coordinates of the intersection. String format: ["x,y","x1,y1","x2,y2"] ''' point = [] point1 = [] # first convert to a list for i in range(len(point_list)): point.append(list(point_list[i])) point[i][0] = round(point[i][0], num) point[i][1] = round(point[i][1], num) # Delete the Z-axis coordinates for j in range(len(point)): if point[j][2] == 0.0: point[j]. remove(point[j][2]) # remove duplicates if point[j] not in point1: point1.append(point[j]) # Convert to string format: ["x,y","x1,y1","x2,y2"] b = "," for k in range(len(point1)): a = str(point1[k][0]) c = str(point1[k][1]) point1[k] = str(a + b + c) print(point1) return point1 def wipe_off_list(self, intersect_point, start_point, end_point): """ Intersection screening to remove T-shaped and right-angle intersections :param intersect_point: all intersection points :param start_point: The coordinates of the starting point of the line :param end_point: coordinates of the end point of the line :return: Return the coordinates of the intersection point """ point = [] for i in range(len(intersect_point)): if intersect_point[i] not in start_point and intersect_point[i] not in end_point: point.append(intersect_point[i]) return point def paixun(self, point_list: list, xy=0): ''' @funtion: Bubble sort, from small to large :param point_list: incoming list :param xy: xy=0 sort x coordinates, xy=1 sort y coordinates :return: return a list sorted from small to large ''' for i in range(1, len(point_list)): for j in range(0, len(point_list) - i): if point_list[j][xy] > point_list[j + 1][xy]: point_list[j][xy], point_list[j + 1][xy] = point_list[j + 1][xy], point_list[j][xy] return point_list def now_time(self, date=True): # Get the current time i = datetime.datetime.now() now_time = str(i) if date: now_time = now_time. replace(" ", "_") now_time = now_time.replace("-", "_") now_time = now_time.replace(":", "_") now_time = now_time. split(".")[0] else: now_time = now_time. split(" ")[0] now_time = now_time.replace("-", ".") return now_time def csv_reader(self, file): # csv file read zhclb = [] # file = r'E:\pythonProject2\fittings.csv' # with open(file, encoding="utf-8") as f: reader = csv.reader(f) # create reader header_row = next(reader) # Get the first row of the table (usually the column name) zhclb.append(header_row) for row in reader: # Loop to get all the data behind zhclb.append(row) print(zhclb) return zhclb