Search the Community
Showing results for tags 'win32com'.
-
I am new to automating drawings in Autocad. I am using win32com to access autocad. I am using AddDimAligned function to add dimension but having trouble placing the text above the dimension line as the error is saying can not be set. Here is my code - class AutoCAD: def __init__(self): try: self.acad = win32com.client.Dispatch("AutoCAD.Application") time.sleep(2) self.acad.Visible = True self.doc = self.acad.ActiveDocument self.modelspace = self.doc.ModelSpace time.sleep(2) self.linetypes = self.doc.Linetypes self.layers = self.doc.Layers try: self.dimensionstyle = self.doc.DimStyles.Item("DIM") except Exception as e: print(f"Error: DIM dimension style not found. Creating it... {e}") # Create a new dimension style 'DIM' if it doesn't exist self.dimensionstyle = self.doc.DimStyles.Add("DIM") print("DIM dimension style created.") if self.modelspace.Count > 0: self.open_new_drawing() except Exception as e: raise AutoCADError(f"Error initializing AutoCAD: {e}") def add_dimension(self, dimension): try: distance_mm = dimension.convert_to_mm() dimension_obj = None if dimension.dimension_type == DimensionType.ALIGNED: dimension_obj = self.modelspace.AddDimAligned(dimension.start_point.to_variant(), dimension.end_point.to_variant(), dimension.text_point.to_variant()) elif dimension.dimension_type == DimensionType.LINEAR: dimension_obj = self.modelspace.AddDimLinear(dimension.start_point.to_variant(), dimension.end_point.to_variant(), dimension.text_point.to_variant()) elif dimension.dimension_type == DimensionType.ANGULAR: dimension_obj = self.modelspace.AddDimAngular(dimension.start_point.to_variant(), dimension.end_point.to_variant(), dimension.text_point.to_variant()) elif dimension.dimension_type == DimensionType.RADIAL: dimension_obj = self.modelspace.AddDimRadial(dimension.start_point.to_variant(), dimension.end_point.to_variant(), dimension.text_point.to_variant()) elif dimension.dimension_type == DimensionType.DIAMETER: dimension_obj = self.modelspace.AddDimDiameter(dimension.start_point.to_variant(), dimension.end_point.to_variant(), dimension.text_point.to_variant()) dimension_obj.TextOverride = f"{distance_mm:.0f}" # Set the dimension text to the calculated distance # self.dimensionstyle.TextAlign = 3 # 3 = centered text # self.dimensionstyle.TextOffset = 5 return dimension_obj except Exception as e: raise AutoCADError(f"Error adding dimension: {e}") def edit_dim_properties(self, dim,height=0.1,gap=0.02, units_precision=2, arrow_head_size=0.1,text_offset=5 ): try: dim.TextHeight = height dim.TextGap = gap dim.PrimaryUnitsPrecision = units_precision dim.ArrowheadSize = arrow_head_size dim.TextOffset = text_offset except Exception as e: raise AutoCADError(f"Error editing dimension properties: {e}")