### Integrate with Excel for Parametric Design in CATIA Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Connects to an active Excel application or starts a new instance to read design parameters from a specified workbook (e.g., 'Parameters.xlsx') and write output data back to cells. Leverages Microsoft.Office.Interop.Excel. Requires COM interop. Enables data-driven parametric modeling in CATIA. ```csharp using EXCEL = Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; EXCEL.Application excel; try { excel = (EXCEL.Application)Marshal.GetActiveObject("EXCEL.Application"); } catch (Exception) { excel = (EXCEL.Application)Activator.CreateInstance(Type.GetTypeFromProgID("EXCEL.Application")); excel.Visible = true; } // Open existing workbook EXCEL.Workbook wb = excel.Workbooks.Open(@"C:\Path\To\Parameters.xlsx"); EXCEL.Worksheet oSheet = excel.ActiveSheet; // Read cell values string parameter = oSheet.Cells[1, 1].Value.ToString(); double value = Convert.ToDouble(oSheet.Cells[2, 1].Value); // Write data to cells oSheet.Cells[1, 1] = "Part Name"; oSheet.Cells[1, 2] = "Volume"; oSheet.Cells[2, 1] = "Component1"; oSheet.Cells[2, 2] = 150.5; // Save and close wb.Save(); wb.Close(); excel.Quit(); ``` -------------------------------- ### Create and Manage Product Assemblies in CATIA V5 with C# Source: https://context7.com/nsaori/catia-v5-automation/llms.txt This C# code demonstrates how to create hierarchical product assemblies in CATIA V5. It includes adding components from files, creating sub-assemblies, applying transformations for component placement, and extracting the Bill of Materials (BOM) to both text and HTML formats. Requires MECMOD and ProductStructureTypeLib references. ```csharp using MECMOD; using ProductStructureTypeLib; ProductDocument pdctDoc = (ProductDocument)catia.Documents.Add("Product"); Product pdct = pdctDoc.Product; Products pdcts = pdct.Products; // Add base component from file object[] tpath = { @"C:\Path\To\Hull.CATPart" }; pdcts.AddComponentsFromFiles(tpath, "*"); // Create sub-assembly with multiple components Product pdct1 = pdcts.AddNewProduct("ass1"); object[] tpath1 = { @"C:\Path\To\Castle.CATPart", @"C:\Path\To\Funnel.CATPart" }; pdct1.Products.AddComponentsFromFiles(tpath1, "*"); // Create copies with transformations Product pdctRef = pdct1.ReferenceProduct; // Transformation array: x-vector(x,y,z), y-vector(x,y,z), z-vector(x,y,z), translation(x,y,z) object[] aDistance = {1, 0, 0, 0, 1, 0, 0, 0, 1, 60, 0, 0}; Product pdct2 = pdcts.AddComponent(pdctRef); pdct2.Move.Apply(aDistance); aDistance[9] = 120; // Change x translation Product pdct3 = pdcts.AddComponent(pdctRef); pdct3.Move.Apply(aDistance); // Extract Bill of Materials pdct.ExtractBOM(CatFileType.catFileTypeText, @"C:\Path\To\Bom.txt"); pdct.ExtractBOM(CatFileType.catFileTypeHTML, @"C:\Path\To\BOM.html"); ``` -------------------------------- ### Connect to CATIA V5 Application using C# Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Establishes a connection to a running CATIA V5 instance or launches a new one via COM interop. This is a prerequisite for all subsequent automation tasks. It returns an INFITF.Application object. ```csharp using System; using System.Runtime.InteropServices; using MECMOD; INFITF.Application catia; try { // Connect to running CATIA instance catia = (INFITF.Application)Marshal.GetActiveObject("CATIA.Application"); } catch (Exception) { // Launch new CATIA instance if not running catia = (INFITF.Application)Activator.CreateInstance(Type.GetTypeFromProgID("CATIA.Application")); catia.Visible = true; } // Create new part document PartDocument prtDoc = (PartDocument)catia.Documents.Add("Part"); Part prt = prtDoc.Part; prt.Update(); ``` -------------------------------- ### Connect to CATIA V5 Application (C#) Source: https://github.com/nsaori/catia-v5-automation/blob/master/drawing/code.txt Establishes a connection to an active CATIA V5 application instance or launches a new one if none is found. This is the primary step for any CATIA automation task. It utilizes COM interop to interact with the CATIA application. ```csharp using System; using System.Runtime.InteropServices; using INFITF; // ... inside a Form class ... public INFITF.Application Catia { get; private set; } private void Form1_Load(object sender, EventArgs e) { try { Catia = (INFITF.Application)Marshal.GetActiveObject("CATIA.Application"); } catch { Catia = (INFITF.Application)Activator.CreateInstance(Type.GetTypeFromProgID("CATIA.Application")); Catia.Visible = true; } } ``` -------------------------------- ### Recursive Product Structure Traversal and Excel Export with Screenshots in CATIA V5 using C# Source: https://context7.com/nsaori/catia-v5-automation/llms.txt This C# code recursively traverses a CATIA V5 product assembly hierarchy, exporting assembly data, custom properties, and screenshots to an Excel spreadsheet. It initializes Excel, captures views of each product, sets camera viewpoints, and inserts images into the sheet. Requires MECMOD, ProductStructureTypeLib, and Microsoft.Office.Interop.Excel references. ```csharp using MECMOD; using ProductStructureTypeLib; using EXCEL = Microsoft.Office.Interop.Excel; public partial class Form1 : Form { INFITF.Application catia = null; ProductDocument prdDoc = null; EXCEL.Application excel = null; EXCEL.Worksheet ws = null; int level = 0; int cellrowNum = 0; private void button2_Click(object sender, EventArgs e) { excel = (EXCEL.Application)Activator.CreateInstance(Type.GetTypeFromProgID("EXCEL.Application")); excel.Visible = true; EXCEL.Workbook wb = excel.Workbooks.Add(); ws = excel.ActiveSheet; prdDoc = (ProductDocument)catia.ActiveDocument; Product prd = prdDoc.Product; re(prd); } private void re(Product prd) { INFITF.Document doc = (INFITF.Document)prd.ReferenceProduct.Parent; string fullPath = doc.FullName; Products pds = prd.Products; cellrowNum++; ws.Cells[cellrowNum, 1] = level; ws.Cells[cellrowNum, 2] = prd.get_PartNumber(); ws.Cells[cellrowNum, 3] = fullPath; ws.Cells[cellrowNum, 4] = prd.Analyze.Volume; // Capture screenshot INFITF.Document tDoc = null; if (level != 0) { tDoc = catia.Documents.Open(fullPath); } INFITF.Window w = catia.ActiveWindow; INFITF.Viewer3D vwr = (INFITF.Viewer3D)w.ActiveViewer; // Set white background object[] clrArry = { 1, 1, 1 }; vwr.PutBackgroundColor(clrArry); vwr.FullScreen = true; vwr.Reframe(); // Set isometric view INFITF.Camera3D camera = (INFITF.Camera3D)doc.Cameras.Item("* iso"); vwr.Viewpoint3D = camera.Viewpoint3D; vwr.Activate(); // Capture to file vwr.CaptureToFile(INFITF.CatCaptureFormat.catCaptureFormatJPEG, fullPath + cellrowNum + ".jpg"); // Insert image in Excel EXCEL.Range ImgRane = ws.Cells[cellrowNum, 5]; ws.Shapes.AddPicture(fullPath + cellrowNum + ".jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, ImgRane.Left + 1, ImgRane.Top + 1, ImgRane.Width - 2.5, ImgRane.Height - 3); vwr.FullScreen = false; if (level != 0) { tDoc.Close(); } // Get custom properties string designer = ""; try { designer = prd.Parameters.Item("designer").ValueAsString(); } catch (Exception) { designer = ""; } ws.Cells[cellrowNum, 6] = designer; // Recursive traversal for (int i = 1; i <= pds.Count; i++) { Product tpdt = pds.Item(i); level++; re(tpdt); level--; } } } ``` -------------------------------- ### 3D Solid Modeling with Boolean Operations in CATIA V5 Source: https://context7.com/nsaori/catia-v5-automation/llms.txt This C# code snippet demonstrates how to create multiple 3D bodies in CATIA V5 using sketches (rectangles, circles, triangles) and pad operations. It then combines these bodies using boolean union (Add) and subtraction (Remove) operations on a main part body. Dependencies include CATIA V5 automation libraries like MECMOD and PARTITF. ```csharp using MECMOD; using PARTITF; INFITF.Reference xypln = (INFITF.Reference)prt.OriginElements.PlaneXY; ShapeFactory shpfac = (ShapeFactory)prt.ShapeFactory; Bodies bdys = prt.Bodies; Body PartBody = bdys.Item(1); // Create body 1 with rectangle and pad Body bdy1 = bdys.Add(); Sketch skt1 = bdy1.Sketches.Add(xypln); Factory2D fac2d = skt1.OpenEdition(); CreateRectangle(fac2d, prt, skt1, 50, 50, 100, 100); skt1.CloseEdition(); Pad p1 = shpfac.AddNewPad(skt1, 80); // Create body 2 with circle and pad Body bdy2 = bdys.Add(); Sketch skt2 = bdy2.Sketches.Add(xypln); fac2d = skt2.OpenEdition(); Circle2D c = fac2d.CreateClosedCircle(75, 75, 20); skt2.CloseEdition(); Pad p2 = shpfac.AddNewPad(skt2, 100); // Create body 3 with triangle and pad Body bdy3 = bdys.Add(); Sketch skt3 = bdy3.Sketches.Add(xypln); fac2d = skt3.OpenEdition(); Point2D pt1 = fac2d.CreatePoint(75, 80); Point2D pt2 = fac2d.CreatePoint(70, 70); Point2D pt3 = fac2d.CreatePoint(80, 70); CreateTriangle(fac2d, prt, skt3, pt1, pt2, pt3); // Assuming CreateTriangle is defined elsewhere skt3.CloseEdition(); Pad p3 = shpfac.AddNewPad(skt3, 100); // Set PartBody as working object and perform boolean operations prt.InWorkObject = PartBody; shpfac.AddNewAdd(bdy1); // Boolean union with body 1 shpfac.AddNewAdd(bdy2); // Boolean union with body 2 shpfac.AddNewRemove(bdy3); // Boolean subtraction with body 3 prt.Update(); ``` -------------------------------- ### Read Point Cloud from Text File and Create 3D Points in CATIA Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Reads point coordinates from a comma-separated text file (e.g., 'point.txt') and creates corresponding 3D points within CATIA's Generative Shape Design workbench. Requires System.IO and CATIA V5 COM interop libraries. Outputs 3D points in the active part document. ```csharp using System.IO; using MECMOD; using HybridShapeTypeLib; PartDocument prtdoc = (PartDocument)catia.Documents.Add("Part"); Part prt = prtdoc.Part; HybridBodies hbdys = prt.HybridBodies; HybridBody hbdy = hbdys.Add(); HybridShapeFactory hsFac = (HybridShapeFactory)prt.HybridShapeFactory; try { StreamReader sr = new StreamReader(@"C:\Path\To\point.txt"); String line = ""; while ((line = sr.ReadLine()) != null) { string[] token = line.Split(','); int x = int.Parse(token[0]); int y = int.Parse(token[1]); int z = int.Parse(token[2]); Point p = hsFac.AddNewPointCoord(x, y, z); hbdy.AppendHybridShape(p); } sr.Close(); prt.Update(); } catch (Exception e) { Console.WriteLine("File I/O error: " + e.Message); } ``` -------------------------------- ### Automate Technical Drawings in CATIA V5 Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Automates the creation of technical drawings, including adding multiple views, text annotations with leaders, and tables from a 3D CAD part. It requires the MECMOD and DRAFTINGITF namespaces. The script opens a part and a drawing template, defines front and top views, adds 2D elements to these views, and populates a table. ```csharp using MECMOD; using DRAFTINGITF; // Open 3D part and drawing template PartDocument prtDoc = (PartDocument)catia.Documents.Open(@"C:\Path\To\Bolt.CATPart"); DrawingDocument drwDoc = (DrawingDocument)catia.Documents.Open(@"C:\Path\To\TitleBlock.CATDrawing"); DrawingSheet drwSheet = drwDoc.DrawingRoot.ActiveSheet; // Create front view DrawingView FrntView = drwSheet.Views.Add("Front View"); FrntView.x = 400; FrntView.y = 150; // Configure front view with XY plane orientation DrawingViewGenerativeBehavior FrntVwGB = FrntView.GenerativeBehavior; FrntVwGB.DefineFrontView(1, 0, 0, 0, 1, 0); FrntView.GenerativeBehavior.Document = prtDoc; // Create top view as projection DrawingView Topvw = drwSheet.Views.Add("TopView"); Topvw.x = 400; Topvw.y = 600; DrawingViewGenerativeBehavior TopVwGB = Topvw.GenerativeBehavior; TopVwGB.DefineProjectionView(TopVwGB, CatProjViewType.catTopView); Topvw.GenerativeBehavior.Document = prtDoc; // Add 2D circles in views FrntView.Activate(); Factory2D facF = FrntView.Factory2D; Circle2D c1 = facF.CreateClosedCircle(30, 30, 10); Topvw.Activate(); Factory2D facT = Topvw.Factory2D; Circle2D c2 = facT.CreateClosedCircle(30, 30, 20); // Add text annotations with leaders FrntView.Activate(); DrawingText Ftxt = FrntView.Texts.Add("Front View", 100, 100); Ftxt.SetFontSize(0, 0, 12); DrawingLeader FDleadr = Ftxt.Leaders.Add(0, 0); Topvw.Activate(); DrawingText Ttxt = Topvw.Texts.Add("Top View", -45, 130); Ttxt.SetFontSize(0, 0, 12); DrawingLeader TDleadr = Ttxt.Leaders.Add(0, 0); // Add table (x, y, rows, columns, row height, column width) DrawingTable table = FrntView.Tables.Add(100, 80, 2, 3, 14, 40); drwDoc.Update(); ``` -------------------------------- ### Create 3D Wireframe and Surfaces in CATIA V5 Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Generates 3D wireframe geometry (points, splines) and a swept surface using the Generative Shape Design workbench in CATIA V5. It requires the MECMOD and HybridShapeTypeLib namespaces. The script creates points, splines from these points, and a surface swept between two splines, projecting a point onto the created surface. ```csharp using MECMOD; using HybridShapeTypeLib; PartDocument prtDoc = (PartDocument)catia.Documents.Add("Part"); Part prt = prtDoc.Part; // Create hybrid body for wireframe elements HybridBodies hybdys = prt.HybridBodies; HybridBody hybdy = hybdys.Add(); HybridShapeFactory hyshfac = (HybridShapeFactory)prt.HybridShapeFactory; // Create points for first spline Point p1 = hyshfac.AddNewPointCoord(10, 60, 30); Point p2 = hyshfac.AddNewPointCoord(70, 75, 35); Point p3 = hyshfac.AddNewPointCoord(100, 80, 30); // Create points for second spline Point p4 = hyshfac.AddNewPointCoord(100, 80, 40); Point p5 = hyshfac.AddNewPointCoord(95, 20, 45); Point p6 = hyshfac.AddNewPointCoord(100, 10, 50); // Create references from geometry INFITF.Reference r1 = prt.CreateReferenceFromGeometry(p1); INFITF.Reference r2 = prt.CreateReferenceFromGeometry(p2); INFITF.Reference r3 = prt.CreateReferenceFromGeometry(p3); INFITF.Reference r4 = prt.CreateReferenceFromGeometry(p4); INFITF.Reference r5 = prt.CreateReferenceFromGeometry(p5); INFITF.Reference r6 = prt.CreateReferenceFromGeometry(p6); // Create splines HybridShapeSpline hyspl1 = hyshfac.AddNewSpline(); hyspl1.AddPoint(r1); hyspl1.AddPoint(r2); hyspl1.AddPoint(r3); HybridShapeSpline hyspl2 = hyshfac.AddNewSpline(); hyspl2.AddPoint(r4); hyspl2.AddPoint(r5); hyspl2.AddPoint(r6); // Add splines to hybrid body hybdy.AppendHybridShape(hyspl1); hybdy.AppendHybridShape(hyspl2); // Create swept surface between splines INFITF.Reference rspl1 = prt.CreateReferenceFromGeometry(hyspl1); INFITF.Reference rspl2 = prt.CreateReferenceFromGeometry(hyspl2); HybridShapeSweepExplicit swp = hyshfac.AddNewSweepExplicit(rspl1, rspl2); hybdy.AppendHybridShape(swp); // Project point onto surface Point p = hyshfac.AddNewPointCoord(50, 30, 100); hybdy.AppendHybridShape(p); HybridShapeProject hsprjct = hyshfac.AddNewProject((INFITF.Reference)p, (INFITF.Reference)swp); prt.Update(); ``` -------------------------------- ### Create 2D Sketch with Constraints in CATIA V5 using C# Source: https://context7.com/nsaori/catia-v5-automation/llms.txt Generates a parametric 2D sketch on the XY plane within a CATIA V5 part document. It includes creating geometric points and lines, and applying a perpendicularity constraint. This functionality requires an active CATIA application connection and a part document. ```csharp using MECMOD; // Get XY plane reference INFITF.Reference xypln = (INFITF.Reference)prt.OriginElements.PlaneXY; // Create body and sketch Bodies bdys = prt.Bodies; Body bdy = bdys.Add(); Sketches skts = bdy.Sketches; Sketch skt = skts.Add(xypln); // Open sketch for editing Factory2D fac2d = skt.OpenEdition(); // Create points Point2D p1 = fac2d.CreatePoint(10, 10); Point2D p2 = fac2d.CreatePoint(10, 40); Point2D p3 = fac2d.CreatePoint(40, 40); Point2D p4 = fac2d.CreatePoint(40, 10); // Create lines Line2D linLft = fac2d.CreateLine(10, 10, 10, 40); Line2D linTop = fac2d.CreateLine(10, 40, 40, 40); Line2D linRgt = fac2d.CreateLine(40, 40, 40, 10); // Set endpoints linLft.StartPoint = p1; linLft.EndPoint = p2; linTop.StartPoint = p2; linTop.EndPoint = p3; linRgt.StartPoint = p3; linRgt.EndPoint = p4; // Add perpendicularity constraint Constraint cnst = skt.Constraints.AddBiEltCst( CatConstraintType.catCstTypeAxisPerpendicularity, (INFITF.Reference)linLft, (INFITF.Reference)linTop); // Close sketch and update part skt.CloseEdition(); prt.Update(); ``` -------------------------------- ### Create Parametric Rectangle Sketch in CATIA V5 Source: https://context7.com/nsaori/catia-v5-automation/llms.txt A reusable C# method to create a constrained rectangular sketch within a CATIA V5 Part document. It takes a 2D factory, part, sketch object, and corner coordinates as input. The method generates points, lines, and applies distance and positioning constraints to define the rectangle parametrically. Dependencies include CATIA V5 automation libraries. ```csharp private static void CreateRectangle(Factory2D fac, Part prt, Sketch skt, double x1, double y1, double x2, double y2) { Point2D p1 = fac.CreatePoint(x1, y1); Point2D p2 = fac.CreatePoint(x2, y1); Point2D p3 = fac.CreatePoint(x2, y2); Point2D p4 = fac.CreatePoint(x1, y2); Line2D lin1 = CreateLine(fac, p1, p2); Line2D lin2 = CreateLine(fac, p2, p3); Line2D lin3 = CreateLine(fac, p3, p4); Line2D lin4 = CreateLine(fac, p4, p1); CatConstraintType cntype = CatConstraintType.catCstTypeDistance; // Add distance constraints between opposite lines Constraint cnst1 = CreateCnst(prt, skt, cntype, lin1, lin3); Constraint cnst2 = CreateCnst(prt, skt, cntype, lin2, lin4); // Add positioning constraints relative to axes Constraint cnst3 = CreateCnst(prt, skt, cntype, lin1, skt.AbsoluteAxis.HorizontalReference); Constraint cnst4 = CreateCnst(prt, skt, cntype, lin2, skt.AbsoluteAxis.VerticalReference); } private static Line2D CreateLine(Factory2D fac, Point2D p1, Point2D p2) { object[] ob1 = new object[2]; p1.GetCoordinates(ob1); object[] ob2 = new object[2]; p2.GetCoordinates(ob2); Line2D line = fac.CreateLine((double)ob1[0], (double)ob1[1], (double)ob2[0], (double)ob2[1]); line.StartPoint = p1; line.EndPoint = p2; return line; } private static Constraint CreateCnst(Part prt, Sketch skt, CatConstraintType cntype, INFITF.AnyObject ob1, INFITF.AnyObject ob2) { INFITF.Reference r1 = prt.CreateReferenceFromGeometry(ob1); INFITF.Reference r2 = prt.CreateReferenceFromGeometry(ob2); Constraint cnt = skt.Constraints.AddBiEltCst(cntype, r1, r2); return cnt; } ``` -------------------------------- ### CATIA Drawing View Projection Calculation (C#) Source: https://github.com/nsaori/catia-v5-automation/blob/master/drawing/code.txt Retrieves projection plane information from a CATIA drawing view and calculates 3D position values. This snippet demonstrates how to access drawing document elements and perform geometric calculations based on view properties. ```csharp using System; using DRAFTINGITF; using ProductStructureTypeLib; using MECMOD; // ... assuming Catia is an initialized INFITF.Application object ... DRAFTINGITF.DrawingDocument DrwDoc = (DRAFTINGITF.DrawingDocument)Catia.ActiveDocument; DRAFTINGITF.DrawingRoot DrwRoot = DrwDoc.DrawingRoot; DRAFTINGITF.DrawingView DrwView = DrwRoot.ActiveSheet.Views.Item(DrwRoot.ActiveSheet.Views.Count); DRAFTINGITF.DrawingViewGenerativeBehavior DrwGenBeh = DrwView.GenerativeBehavior; double X1, Y1, Z1, X2, Y2, Z2; DrwGenBeh.GetProjectionPlane(out X1, out Y1, out Z1, out X2, out Y2, out Z2); // 3D 위치 값 double X = -37.935, Y = 96.8, Z = 104.207; // 결과 값 double COS_ALPHA = 0, VW_H = 0, VW_V = 0; // 계산과정 COS_ALPHA = (X * X1 + Y * Y1 + Z * Z1) / ((Math.Pow(X1, 2) + Math.Pow(Y1, 2) + Math.Pow(Z1, 2)) * Math.Sqrt(Math.Pow(X ,2) + Math.Pow(Y , 2) + Math.Pow(Z , 2))); VW_H = Math.Sqrt(X * X + Y * Y + Z * Z) * COS_ALPHA; COS_ALPHA = (X * X2 + Y * Y2 + Z * Z2) / ((Math.Pow(X2 ,2) + Math.Pow(Y2 , 2) + Math.Pow(Z2 ,2)) * Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y , 2) + Math.Pow(Z , 2))); VW_V = Math.Sqrt(Math.Pow(X , 2) + Math.Pow(Y , 2) + Math.Pow(Z , 2)) * COS_ALPHA; ProductStructureTypeLib.Product Prd = (ProductStructureTypeLib.Product)DrwGenBeh.Document; MECMOD.PartDocument PrtDoc = (MECMOD.PartDocument)Prd.ReferenceProduct.Parent; MECMOD.Part Prt = PrtDoc.Part; MECMOD.Body Bdy = Prt.MainBody; MECMOD.Shapes Shps = Bdy.Shapes; // 'Shps' now contains the shapes of the main body of the part. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.