Rename with c# api with idw

Rename with c# api with idw

talha.tufan523TD
Advocate Advocate
694 Views
3 Replies
Message 1 of 4

Rename with c# api with idw

talha.tufan523TD
Advocate
Advocate

 

I want to change part name and idw name at the same time. Ican change it now, but references do not occur in changed files. I have atached the video of the situation.

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Inventor;
using System.Runtime.InteropServices;


namespace Barida_Power_Tools.Classes
{
   

    public partial class SelectedRename
    {
        private Inventor.Application Application { get; set; }

        public static void GetSelection()
        
        {
            Inventor.Application inventorApp21;
            inventorApp21 = (Application)Marshal.GetActiveObject("Inventor.Application");

           var SelectedComOcc = inventorApp21.ActiveDocument.SelectSet[1] as ComponentOccurrence;
           string oldFilename = SelectedComOcc.ReferencedFileDescriptor.FullFileName;
           FileManager fileManager = inventorApp21.FileManager;
           
            //Create new File new file name
            string newFile = oldFilename.Substring(0, oldFilename.Length - 4) + "_new.ipt";
            string Oldidw = oldFilename.Substring(0, oldFilename.Length - 4) + ".idw";
            string newidw = Oldidw.Substring(0, Oldidw.Length - 4) + "_new.idw";

            try
            {
                fileManager.CopyFile(oldFilename, newFile);
                fileManager.CopyFile(Oldidw, newidw);
            }
            catch { };

            Document idwDoc = inventorApp21.Documents.Open(Oldidw, false);

            var drawingDoc = idwDoc as DrawingDocument;
            PartDocument c = inventorApp21.Documents.Open(newFile, false) as PartDocument;

            foreach (Sheet sheet in drawingDoc.Sheets)
            {
                foreach (DrawingView drawingView in sheet.DrawingViews)
                {
                    var b = drawingView.ReferencedDocumentDescriptor.ReferencedDocument as PartDocument;
                    b = c;
                }
            }



            SelectedComOcc.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(newFile);

            fileManager.DeleteFile(oldFilename);

        }
    }

}

 

0 Likes
Accepted solutions (1)
695 Views
3 Replies
Replies (3)
Message 2 of 4

talha.tufan523TD
Advocate
Advocate

Sorry about my reccord. it didnt catch dropdown and file dialogs. so it seems like work good but actualy didn't. Ipicked the referance file with openfiledialog at the end.  But it didnt seen on reccord. 

0 Likes
Message 3 of 4

A.Acheson
Mentor
Mentor
Accepted solution

Hi @talha.tufan523TD 

It would be better to save as the part document from within the assembly then occurrence.replace the part, save as the drawing and  then file reference replace the part in the drawing. Copying the file in windows is not the correct route to go. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 4 of 4

talha.tufan523TD
Advocate
Advocate
Yes its works fine right now. Thanks a lot.
0 Likes