Hello,
I'm trying to create a new part document, and make a model in it. After creating the part document, I create a part component definition. When trying to add a sketch to the workplane, I get the following error:
'WorkPlanes' does not contain a definition for 'Item' and no accessible extension method 'Item' accepting a first argument of type 'WorkPlanes' could be found (are you missing a directive or an assembly reference?)
My code shows failed attempts at getting the WorkPlane first, but also a failed attempt at just creating a sketch on the plane. All attempts have the same error. Why doesn't WorkPlanes.Item work?
Inv.PartDocument InvDoc = (Inv.PartDocument)g_InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject);
Inv.PartComponentDefinition PrtDef = g_InvDoc. ComponentDefinition;
Inv.WorkPlane Wplane = PrtDef.WorkPlanes.Item(3);
Inv.WorkPlane _Wplane = (Inv.WorkPlane)PrtDef.WorkPlanes.Item(3);
Inv.PlanarSketch CurSketch = PrtDef.Sketches.Add(PrtDef.WorkPlanes.Item(3));
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
I don't know if this will help, but I tried this with an iLogic / vb.net rule and it worked. I didn't try your code as C#/add-in, but I don't see any issue with it.
Edit: maybe see the example here for a C# workplanes example:
InvDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject) PrtDef = InvDoc.ComponentDefinition Wplane = PrtDef.WorkPlanes.Item(3) CurSketch = PrtDef.Sketches.Add(Wplane) CurSketch.name = "Test1" CurSketch = PrtDef.Sketches.Add(PrtDef.WorkPlanes.Item(3)) CurSketch.name = "Test2"
It is about C# syntax. Access to the item in collection should be in [square brackets] instead of (round brackets)
// PrtDef.WorkPlanes.Item(3);
// Should be
PrtDef.WorkPlanes.Item[3];
Thanks for testing. I didn't mention this, but I am building an exe instead of an addin. does that change how to get a workplane or start a sketch? I have included more code below for clarity.
Thanks for replying. I started switching from vb.net to c# 6 months ago and I am still getting used to the C# syntax. Thank you. However, after trying the square brackets I still get the error.
*More info:
I am referencing the Inventor interop: C:\Program Files\Autodesk\Inventor 2024\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll
Target Framework: .NET Framework 4.7.2
using Inv = Inventor;
namespace SimplifiedValve
{
public partial class MainForm : WinFrm.Form
{
public static Inv.Application g_InvApp;
public static Inv.PartDocument g_InvDoc;
private void MainForm_Load(object sender, EventArgs e)
{
InitiateConstants();
// Setup the form
FormSetup();
//this.Show();
// Initiate Inventor
InitInventor();
// Make the filename
UpdateFileName();
// Model the valve
//ModelValve();
}
#region Initialize Inventor Application
/// <summary>
/// Load this to initialize the Inventor application before using it globally.
/// </summary>
public void InitInventor()
{
// Open a session if it doesn't exist
// Try gettin a running Inventor session
try
{
g_InvApp = (Inv.Application)SysInter.Marshal.GetActiveObject("Inventor.Application"); // Get the running Inventor session
g_InvDoc = (Inv.AssemblyDocument)g_InvApp.ActiveDocument; // Get the active document
}
catch (Exception ex) { TryAgain("App"); }
// Try to get the document again if it wasn't captured
if (g_InvDoc == null) { TryAgain("Doc"); }
}
#endregion
#region ModelValve
/// <summary>
/// Builds the model in Inventor.
/// </summary>
public void ModelValve()
{
Inv.PartDocument InvDoc = (Inv.PartDocument)g_InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject);
Inv.PartComponentDefinition PrtDef = g_InvDoc.ComponentDefinition;
Inv.WorkPlane Wplane = PrtDef.WorkPlanes.Item[3];
Inv.WorkPlane _Wplane = (Inv.WorkPlane)PrtDef.WorkPlanes.Item[3];
Inv.PlanarSketch CurSketch = PrtDef.Sketches.Add(PrtDef.WorkPlanes.Item[3]);
//PrtDef.Sketches.Add()
}
#endregion
}
}
@snappyjazz, I didn't test, but using the link I posted above for reference I would try this:
PrtDef.WorkPlanes[3]
nv.PlanarSketch CurSketch = PrtDef.Sketches.Add(PrtDef.WorkPlanes[3]);
Can't find what you're looking for? Ask the community or share your knowledge.