Inventor API - C# Visual Studio

Inventor API - C# Visual Studio

christoph.steidle
Participant Participant
10,248 Views
21 Replies
Message 1 of 22

Inventor API - C# Visual Studio

christoph.steidle
Participant
Participant

Hello all,

I am desperately trying to find a simple code and hope that you can help me out here.

 

public Application inventor;
public Document document;

 inventor = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            document = inventor.ActiveDocument;
            PartDocument doc = (PartDocument)inventor.ActiveDocument;
            //PartDocument doc = (PartDocument)inventor.Documents.Add(DocumentTypeEnum.kPartDocumentObject, inventor.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject), true);

            var _count = doc.ComponentDefinition.Features.Count;
            var _count2 = doc.ComponentDefinition.Features.ExtendFeatures.Count;
            var _count3 = doc.ComponentDefinition.Features.ExtrudeFeatures.Count;

            foreach (var feature in doc.ComponentDefinition.Features)
            {
                Console.WriteLine(feature.ToString());
                var List = feature;
            }

I would like to establish a connection to Inventor via VS and C#.
This has already worked.

Now I simply want to count all components and assemblies and save them in a list.

Unfortunately, this does not work.
I suspect there is already an error with PartDocument?

Unfortunately, I am stuck here and am grateful for any help.

 

Greetings

0 Likes
Accepted solutions (1)
10,249 Views
21 Replies
Replies (21)
Message 21 of 22

JelteDeJong
Mentor
Mentor
Accepted solution

I tried this code and it works for me:

JelteDeJong_0-1638311039421.png

 

public void Main()
{
    Inventor.Application inventor = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
    ExportAllDocument2Jt(inventor.ActiveDocument);
}

/// <summary>
/// Exports documents as JT files
/// including refrenced files
/// (excluding assembly files)
/// </summary>
public void ExportAllDocument2Jt(Document doc)
{
    if (IsPartDocumet(doc)) ExportDocument2Jt((PartDocument)doc);
    foreach (Document refDoc in doc.AllReferencedDocuments)
    {
        if (IsPartDocumet(refDoc)) ExportDocument2Jt((PartDocument)refDoc);
    }
}

private bool IsPartDocumet(Document doc)
{
    return (doc.DocumentType == DocumentTypeEnum.kPartDocumentObject);
}

/// <summary>
/// Exports part document as JT files
/// </summary>
public void ExportDocument2Jt(PartDocument doc)
{
    TranslatorAddIn translateAddIn = (TranslatorAddIn)inventor.ApplicationAddIns.ItemById["{16625A0E-F58C-4488-A969-E7EC4F99CACD}"];
    TranslationContext translateContext = inventor.TransientObjects.CreateTranslationContext();
    translateContext.Type = IOMechanismEnum.kFileBrowseIOMechanism;
    NameValueMap nameValueMap = inventor.TransientObjects.CreateNameValueMap();
    DataMedium dataMedium = inventor.TransientObjects.CreateDataMedium();

    if (!System.IO.Directory.Exists(path))
        System.IO.Directory.CreateDirectory(path);

    string fileName = System.IO.Path.GetFileNameWithoutExtension(doc.FullFileName) + ".jt";
    dataMedium.FileName = System.IO.Path.Combine(path, fileName);
    translateAddIn.SaveCopyAs(doc, translateContext, nameValueMap, dataMedium);

}

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 22 of 22

christoph.steidle
Participant
Participant

Thanks for your answer now it works! 🙂

0 Likes