Hello,
I am fairly new to using the Inventor API and trying to work out how to do just basic functions using C#. I am having a lot of difficulty finding good resources for C# as everything seems to be written in VBA for examples. All my code is trying to do is access an open assembly document, import two .ipt files, and add mate constraints between the two parts. The first part is just a thin-walled, hollow cylinder and the second part is a thin disk. I search the faces for each part and find the planar faces which should be the flat part of the disk and the rim of the cylinder, and try to add a simple constraint between them. Then I search for cylindrical faces and try to add a concentric mate constraint as well.
The issue I am having is I do not know how to properly make the concentric mate constraint. Running the program adds both constraints as planar mates and it gives a Inconsistency warning on the second one. I need help manually creating a concentric mate for the cylindrical faces in my code.
Here is my code:
using System;
using Inventor;
namespace AssemblyConstraintsExample
{
class Program
{
static void AddPipeAndFlange(string pipeFileName, string flangeFileName)
{
MateConstraint mateConstraint;
MateConstraint mateConstraint1;
// Get the Inventor application
Inventor.Application inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;
// Get the active assembly document
AssemblyDocument assemblyDoc = inventorApp.ActiveDocument as AssemblyDocument;
// Make sure the document is an assembly
if (assemblyDoc == null)
{
throw new InvalidOperationException("No assembly document is active.");
}
// Get the transient geometry object
TransientGeometry transGeometry = inventorApp.TransientGeometry;
// Add the pipe part
ComponentOccurrence pipeOccurrence = assemblyDoc.ComponentDefinition.Occurrences.Add(pipeFileName, transGeometry.CreateMatrix());
// Add the flange part
ComponentOccurrence flangeOccurrence = assemblyDoc.ComponentDefinition.Occurrences.Add(flangeFileName, transGeometry.CreateMatrix());
// Get the end face of the cylinder (pipe)
// Get the part document from the pipe occurrence
PartDocument pipePartDoc = pipeOccurrence.Definition.Document as PartDocument;
// Iterate through the faces and find the cylindrical face
Face pipeFace = null;
Face pipeCylinderFace = null;
foreach (Face face in pipePartDoc.ComponentDefinition.SurfaceBodies[1].Faces)
{
if (face.SurfaceType == SurfaceTypeEnum.kPlaneSurface)
{
pipeFace = face;
break;
}
}
if (pipeFace == null)
{
throw new InvalidOperationException("Planar face not found in pipe part.");
}
foreach (Face face in pipePartDoc.ComponentDefinition.SurfaceBodies[1].Faces)
{
if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
{
pipeCylinderFace = face;
break;
}
}
if (pipeCylinderFace == null)
{
throw new InvalidOperationException("Planar face not found in pipe part.");
}
// Get the part document from the flange occurrence
PartDocument flangePartDoc = flangeOccurrence.Definition.Document as PartDocument;
// Iterate through the faces and find the planar face
Face flangeFace = null;
Face flangeCylinderFace = null;
foreach (Face face in flangePartDoc.ComponentDefinition.SurfaceBodies[1].Faces)
{
if (face.SurfaceType == SurfaceTypeEnum.kPlaneSurface)
{
flangeFace = face;
break;
}
}
if (flangeFace == null)
{
throw new InvalidOperationException("Planar face not found in flange part.");
}
foreach (Face face in flangePartDoc.ComponentDefinition.SurfaceBodies[1].Faces)
{
if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
{
flangeCylinderFace = face;
break;
}
}
// Check that you found the correct face on the pipe
if (flangeCylinderFace != null)
{
Console.WriteLine("Pipe Face Area: " + pipeFace.Evaluator.Area);
Console.WriteLine("Pipe Face SurfaceType: " + pipeFace.SurfaceType);
}
else
{
Console.WriteLine("Pipe face not found!");
}
// Check that you found the correct face on the flange
if (flangeFace != null)
{
Console.WriteLine("Flange Face Area: " + flangeFace.Evaluator.Area);
Console.WriteLine("Flange Face SurfaceType: " + flangeFace.SurfaceType);
}
else
{
Console.WriteLine("Flange face not found!");
}
// Create references to the pipe and flange faces
AssemblyComponentDefinition assemblyCompDef = assemblyDoc.ComponentDefinition;
// Get the assembly constraints
// Create references to the pipe and flange faces
Object pipeFaceProxy;
pipeOccurrence.CreateGeometryProxy(pipeFace, out pipeFaceProxy);
Object flangeFaceProxy;
flangeOccurrence.CreateGeometryProxy(flangeFace, out flangeFaceProxy);
Object pipeCylinderFaceProxy;
pipeOccurrence.CreateGeometryProxy(pipeCylinderFace, out pipeCylinderFaceProxy);
Object flangeFaceCylinderProxy;
flangeOccurrence.CreateGeometryProxy(flangeCylinderFace, out flangeFaceCylinderProxy);
// Get the assembly constraints
AssemblyConstraints constraints = assemblyCompDef.Constraints;
try
{
// Add a mate constraint between the pipe and flange
mateConstraint = constraints.AddMateConstraint(pipeFaceProxy, flangeFaceProxy, 0.0, InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference, null, null);
mateConstraint1 = constraints.AddMateConstraint(pipeCylinderFaceProxy, flangeFaceCylinderProxy, 0.0, InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference, null, null);
}
catch (Exception ex)
{
Console.WriteLine("Unable to add mate constraint: " + ex.Message);
}
}
static void Main(string[] args)
{
AddPipeAndFlange("C:\\Users\\Liam\\Desktop\\Parts\\Pipe.ipt", "C:\\Users\\Liam\\Desktop\\Parts\\Disk.ipt");
}
}
}
Any help would be appreciated!!
Can't find what you're looking for? Ask the community or share your knowledge.