6.4 尺寸标注与注释的自动化
尺寸标注是工程图的关键,API 支持创建尺寸标注、形位公差、文本注释等。
6.4.1 创建尺寸标注
尺寸标注包括模型尺寸、草图尺寸、自定义尺寸等,可通过Dimensions集合创建:
// 创建模型尺寸标注(从零件模型关联到工程图) public void CreateModelDimensions(Sheet sheet, BaseView baseView) { try { // 获取零件文档的模型参数 PartDocument partDoc = (PartDocument)baseView.ReferencedDocumentDescriptor.ReferencedDocument; Parameters parameters = partDoc.ComponentDefinition.Parameters; // 遍历模型参数,创建尺寸标注 foreach (Parameter param in parameters) { if (param.Type == ParameterTypeEnum.kModelParameter) { ModelParameter modelParam = (ModelParameter)param; // 在基础视图中创建尺寸标注 DrawingDimension dim = sheet.Dimensions.AddModelDimension( modelParam, // 模型参数 baseView, // 关联视图 _inventorApp.TransientGeometry.CreatePoint(0, 0, 0) // 标注位置(自动调整) ); dim.Value = modelParam.Value; } } } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建模型尺寸失败:" + ex.Message); } } // 创建自定义尺寸标注(距离标注) public void CreateCustomDimension(Sheet sheet, BaseView baseView, Point p1, Point p2) { try { // 获取视图中的几何对象(如边) // 此处简化为直接创建两点距离标注 DrawingDimension dim = sheet.Dimensions.AddDistanceDimension( p1, // 起点 p2, // 终点 DimensionOrientationEnum.kHorizontalDimension, // 方向 _inventorApp.TransientGeometry.CreatePoint((p1.X + p2.X) / 2, p1.Y - 10, 0) // 标注位置 ); dim.Value = Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2)); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建自定义尺寸失败:" + ex.Message); } }6.4.2 创建形位公差与表面粗糙度
(1)创建形位公差
// 创建形位公差标注 public void CreateGeometricTolerance(Sheet sheet, BaseView baseView, Edge edge) { try { // 创建形位公差对象 GeometricTolerance geoTol = sheet.Annotations.GeometricTolerances.Add(); // 设置公差类型(如垂直度) geoTol.FeatureControlFrame.GeometricCharacteristicType = GeometricCharacteristicTypeEnum.kPerpendicularityCharacteristic; // 设置公差值 geoTol.FeatureControlFrame.Tolerance.Value = 0.05; // 关联到视图中的边 geoTol.AttachToGeometry(edge, baseView); // 设置放置位置 geoTol.Position = _inventorApp.TransientGeometry.CreatePoint(edge.Geometry.PointOnEdge.X, edge.Geometry.PointOnEdge.Y - 10, 0); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建形位公差失败:" + ex.Message); } }(2)创建表面粗糙度标注
// 创建表面粗糙度标注 public void CreateSurfaceFinish(Sheet sheet, BaseView baseView, Face face) { try { // 创建表面粗糙度对象 SurfaceFinishSymbol surfFinish = sheet.Annotations.SurfaceFinishSymbols.Add(); // 设置粗糙度值 surfFinish.RoughnessValue = 3.2; // 关联到视图中的面 surfFinish.AttachToGeometry(face, baseView); // 设置放置位置 surfFinish.Position = _inventorApp.TransientGeometry.CreatePoint(face.Geometry.PointOnFace.X, face.Geometry.PointOnFace.Y + 10, 0); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建表面粗糙度失败:" + ex.Message); } }6.4.3 创建文本注释与焊接符号
// 创建文本注释 public void CreateTextAnnotation(Sheet sheet, Point position, string text) { try { // 创建文本注释 TextAnnotation textAnn = sheet.Annotations.TextAnnotations.Add(); // 设置文本内容 textAnn.Text = text; // 设置字体和大小 textAnn.Style.TextStyle.FontSize = 3.5; textAnn.Style.TextStyle.FontName = "宋体"; // 设置放置位置 textAnn.Position = position; } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建文本注释失败:" + ex.Message); } }6.5 工程图表格的创建与填充
工程图中的表格包括明细栏、标题栏、参数表等,API 支持创建表格并批量填充数据。
6.5.1 创建明细栏(BOM 表)
明细栏基于装配体的零部件数据自动生成:
// 创建装配体明细栏 public void CreateBOMTable(DrawingDocument drawDoc, Sheet sheet, AssemblyDocument assyDoc, Point position) { try { // 获取BOM表样式 BOMTableStyle bomStyle = drawDoc.StylesManager.BOMTableStyles["默认"]; // 创建BOM表 BOMTable bomTable = sheet.Tables.AddBOMTable( assyDoc, // 装配体文档 position, // 放置位置 bomStyle, // 表格样式 BOMTableTypeEnum.kPartsOnlyBOMTableType, // 表格类型(仅零件) true // 显示数量 ); // 更新BOM表数据 bomTable.Update(); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建BOM表失败:" + ex.Message); } }6.5.2 创建自定义表格
自定义表格可用于参数表、技术要求表等,支持手动添加行和列:
// 创建自定义表格 public void CreateCustomTable(Sheet sheet, Point position, int rows, int cols) { try { // 创建表格 CustomTable customTable = sheet.Tables.AddCustomTable( position, // 放置位置 rows, // 行数 cols, // 列数 15, // 行高 50 // 列宽 ); // 设置表格标题 customTable.Title = "零件参数表"; // 填充表头 customTable.Cell(1, 1).Text = "参数名称"; customTable.Cell(1, 2).Text = "参数值"; customTable.Cell(1, 3).Text = "单位"; // 填充数据 customTable.Cell(2, 1).Text = "长度"; customTable.Cell(2, 2).Text = "100"; customTable.Cell(2, 3).Text = "mm"; customTable.Cell(3, 1).Text = "宽度"; customTable.Cell(3, 2).Text = "50"; customTable.Cell(3, 3).Text = "mm"; } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建自定义表格失败:" + ex.Message); } }6.6 工程图的导出与打印
API 支持将工程图导出为 DWG、PDF、DXF 等格式,也可实现批量打印。
6.6.1 导出为 PDF 格式
// 导出工程图为PDF public void ExportToPDF(DrawingDocument drawDoc, string savePath) { try { // 定义导出选项 PDFExportOptions pdfOptions = _inventorApp.ApplicationOptions.SaveOptions.PDFExportOptions; pdfOptions.ExportQuality = PDFExportQualityEnum.kHighPDFExportQuality; pdfOptions.IncludeLayerInformation = true; // 导出PDF drawDoc.SaveAs(savePath, false); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("导出PDF失败:" + ex.Message); } }6.6.2 批量打印工程图
// 批量打印工程图 public void PrintDrawing(DrawingDocument drawDoc, string printerName) { try { // 定义打印选项 PrintOptions printOptions = _inventorApp.ApplicationOptions.PrintOptions; printOptions.PrinterName = printerName; printOptions.NumberOfCopies = 1; printOptions.PrintRange = PrintRangeEnum.kAllSheets; // 打印 drawDoc.Print(); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("打印工程图失败:" + ex.Message); } }6.7 工程图自动化实例:批量生成零件工程图
以下是一个实例,实现从指定文件夹中批量读取零件文档,自动创建工程图并生成基础视图、尺寸标注和 BOM 表:
// 批量生成零件工程图 public void BatchCreateDrawing(string partFolderPath, string drawSavePath) { try { // 获取文件夹中的所有零件文件 string[] partFiles = System.IO.Directory.GetFiles(partFolderPath, "*.part"); foreach (string partFile in partFiles) { // 创建工程图文档 DrawingDocument drawDoc = CreateDrawingDocument(); if (drawDoc == null) continue; // 添加图纸 Sheet sheet = AddSheet(drawDoc, "零件图"); // 关联零件文档 AddReference(drawDoc, partFile); // 创建基础视图 Point baseViewPos = _inventorApp.TransientGeometry.CreatePoint(100, 100, 0); BaseView baseView = CreateBaseView(drawDoc, sheet, partFile, baseViewPos); // 创建投影视图 Point projectedViewPos = _inventorApp.TransientGeometry.CreatePoint(300, 100, 0); CreateProjectedView(sheet, baseView, projectedViewPos); // 创建尺寸标注 CreateModelDimensions(sheet, baseView); // 创建自定义表格 Point tablePos = _inventorApp.TransientGeometry.CreatePoint(100, 250, 0); CreateCustomTable(sheet, tablePos, 5, 3); // 保存工程图 string fileName = System.IO.Path.GetFileNameWithoutExtension(partFile); drawDoc.SaveAs($@"{drawSavePath}\{fileName}.idw", false); // 关闭文档 drawDoc.Close(true); } _inventorApp.UserInterfaceManager.MessageBox.Show("批量生成工程图完成!"); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("批量生成工程图失败:" + ex.Message); } }