Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rename with c# api with idw

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
talha.tufan523TD
511 Views, 3 Replies

Rename with c# api with idw

 

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);

        }
    }

}

 

Tags (3)
3 REPLIES 3
Message 2 of 4

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. 

Message 3 of 4

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

Yes its works fine right now. Thanks a lot.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report