Beispiel #1
0
        public void ReplaceReferences(AssemblyDocument targetAssembly, TupleList <string, string> namePair, string folderPath)
        {
            OccurrenceList newOccs               = new OccurrenceList(targetAssembly);
            string         pathString            = folderPath;
            List <string>  patternComponentsList = new List <string>();

            for (int i = 0; i < newOccs.Items.Count; i++)
            {
                if (newOccs.Items[i].DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject)
                {
                    for (int f = 0; f < namePair.Count; f++)
                    {
                        if (namePair[f].Item1 == newOccs.Items[i].ReferencedFileDescriptor.FullFileName)
                        {
                            if (patternComponentsList.Contains(namePair[f].Item1))
                            {
                                newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[f].Item2);
                            }

                            else
                            {
                                if (!System.IO.File.Exists(namePair[f].Item2))
                                {
                                    PartDocument partDoc = (PartDocument)PersistenceManager.InventorApplication.Documents.Open(namePair[f].Item1, false);
                                    partDoc.SaveAs(namePair[f].Item2, true);
                                    partDoc.Close(true);
                                    newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[f].Item2);
                                }
                                patternComponentsList.Add(namePair[f].Item1);
                            }
                        }
                    }
                }

                else if (newOccs.Items[i].DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    for (int n = 0; n < namePair.Count; n++)
                    {
                        if (namePair[n].Item1 == newOccs.Items[i].ReferencedFileDescriptor.FullFileName)
                        {
                            AssemblyDocument subAssembly = (AssemblyDocument)PersistenceManager.InventorApplication.Documents.Open(newOccs.Items[i].ReferencedFileDescriptor.FullFileName, false);
                            ReplaceReferences(subAssembly, namePair, pathString);
                            string newFilePath = namePair[n].Item2;
                            subAssembly.SaveAs(namePair[n].Item2, true);
                            subAssembly.Close(true);
                            newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[n].Item2);
                        }
                    }
                }
            }
        }
        //TODO: MakeInvCopy is going to be called over and over again by DesignScript, not us, so there is no opportunity to pass the count into this method.  
        //UniqueModuleEvaluator needs to be modified each time this is called so we know which module we are on.

        //TODO:  ApprenticeServer instance creation and lifetime management needs to be handled by InventorServices.Persistance

        //TODO: OccurrenceList needs to be set on each Module instance during UniqueModuleEvaluator's construction.

        //TODO: Refactor this method, it is so big.
        private void MakeInvCopy(ApprenticeServer appServ, 
                                 string templateAssemblyPath, 
                                 string templateDrawingPath, 
                                 string targetDirectory, 
                                 OccurrenceList occList, 
                                 int count, 
                                 UniqueModuleEvaluator uniqueModuleEvaluator)
        {
            // TODO Test for the existance of folders and assemblies.
            ApprenticeServer oAppServ = appServ;
            int panelID = count;
            OccurrenceList oOccs = occList;
            string topFileFullName;
            string targetPath = targetDirectory;
            TemplateAssemblyPath = templateAssemblyPath;
            TemplateDrawingPath = templateDrawingPath;
            string panelIDString = System.Convert.ToString(panelID);
            UniqueModules = uniqueModuleEvaluator;

            //Instead of using "panelID" to create unique folders for all instances, redirect to the GeometryMapIndex
            string geoMapString = System.Convert.ToString(GeometryMapIndex);
            string folderName;
            if (CreateAllCopies == false)
            {
                if (GeometryMapIndex < 10)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 00" + geoMapString;
                }

                else if (10 <= GeometryMapIndex && GeometryMapIndex < 100)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 0" + geoMapString;
                }
                else
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " " + geoMapString;
                }
            }

            else
            {
                if (panelID < 10)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 00" + panelIDString;
                }
                else if (10 <= panelID && panelID < 100)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 0" + panelIDString;
                }
                else
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " " + panelIDString;
                }
            }
            //if(panelID < 10){
            //Need to get number of the parent occ, top level name as foldername
            string pathString = System.IO.Path.Combine(targetPath, folderName);

            topFileFullName = oOccs.TargetAssembly.FullDocumentName;
            string topFileNameOnly = System.IO.Path.GetFileName(topFileFullName);
            ModulePath = System.IO.Path.Combine(pathString, topFileNameOnly);


            TupleList<string, string> filePathPair = new TupleList<string, string>();

            for (int i = 0; i < occList.Items.Count; i++)
            {
                string targetOccPath = occList.Items[i].ReferencedFileDescriptor.FullFileName;
                string newCopyName = System.IO.Path.GetFileName(targetOccPath);
                string newFullCopyName = System.IO.Path.Combine(pathString, newCopyName);
                filePathPair.Add(targetOccPath, newFullCopyName);
            }

            //Check if an earlier panel already made the folder, if not, create it.
            if (!System.IO.Directory.Exists(pathString))
            {
                firstTime = true;
                System.IO.Directory.CreateDirectory(pathString);
                //AssemblyReplaceRef(oAppServ, oOccs.TargetAssembly, filePathPair, pathString);
                ApprenticeServerDocument oAssDoc;
                oAssDoc = oAppServ.Open(TemplateAssemblyPath);
                FileSaveAs fileSaver;
                fileSaver = oAppServ.FileSaveAs;
                fileSaver.AddFileToSave(oAssDoc, ModulePath);
                fileSaver.ExecuteSaveCopyAs();

                //Need to copy presentation files if there are any.  For now this is only going to work with the top assembly.
                string templateDirectory = System.IO.Path.GetDirectoryName(TemplateAssemblyPath);
                string[] presentationFiles = System.IO.Directory.GetFiles(templateDirectory, "*.ipn");
                //If we want the ability to have subassemblies with .ipn files or multiple ones, this will have to be changed
                //to iterate over all the .ipn files.
                if (presentationFiles.Length != 0)
                {
                    string newCopyPresName = System.IO.Path.GetFileName(presentationFiles[0]);
                    string newFullCopyPresName = System.IO.Path.Combine(pathString, newCopyPresName);

                    ApprenticeServerDocument presentationDocument = oAppServ.Open(presentationFiles[0]);
                    DocumentDescriptorsEnumerator presFileDescriptors = presentationDocument.ReferencedDocumentDescriptors;
                    foreach (DocumentDescriptor refPresDocDescriptor in presFileDescriptors)
                    {
                        if (refPresDocDescriptor.FullDocumentName == TemplateAssemblyPath)
                        {
                            refPresDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModulePath);
                            FileSaveAs fileSavePres;
                            fileSavePres = oAppServ.FileSaveAs;
                            fileSavePres.AddFileToSave(presentationDocument, newFullCopyPresName);
                        }
                    }
                }

                string newCopyDrawingName = System.IO.Path.GetFileName(TemplateDrawingPath);
                string newFullCopyDrawingName = System.IO.Path.Combine(pathString, newCopyDrawingName);

                if (TemplateDrawingPath != "")
                {
                    ApprenticeServerDocument drawingDoc = oAppServ.Open(TemplateDrawingPath);
                    DocumentDescriptorsEnumerator drawingFileDescriptors = drawingDoc.ReferencedDocumentDescriptors;
                    //This needs to be fixed.  It was written with the assumption that only the template assembly would be in 
                    //the details and be first in the collection of document descriptors.  Need to iterate through 
                    //drawingFileDescriptors and match names and replace correct references.
                    //Possibly can use the "filePathPair" object for name matching/reference replacing.
                    //drawingFileDescriptors[1].ReferencedFileDescriptor.ReplaceReference(topAssemblyNewLocation);
                    foreach (DocumentDescriptor refDocDescriptor in drawingFileDescriptors)
                    {
                        foreach (Tuple<string, string> pathPair in filePathPair)
                        {
                            string newFileNameLower = System.IO.Path.GetFileName(pathPair.Item2);
                            string drawingReferenceLower = System.IO.Path.GetFileName(refDocDescriptor.FullDocumentName);
                            string topAssemblyLower = System.IO.Path.GetFileName(ModulePath);
                            if (topAssemblyLower == drawingReferenceLower)
                            {
                                refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModulePath);
                            }
                            if (newFileNameLower == drawingReferenceLower)
                            {
                                refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(pathPair.Item2);
                            }
                        }
                    }

                    FileSaveAs fileSaveDrawing;
                    fileSaveDrawing = oAppServ.FileSaveAs;
                    fileSaveDrawing.AddFileToSave(drawingDoc, newFullCopyDrawingName);
                    fileSaveDrawing.ExecuteSaveCopyAs();
                    firstTime = true;

                    if (!UniqueModules.DetailDocumentPaths.Contains(newFullCopyDrawingName))
                    {
                        UniqueModules.DetailDocumentPaths.Add(newFullCopyDrawingName);
                    }
                }
            }
        }
Beispiel #3
0
        internal void MakeInvCopy(string templateAssemblyPath,
                                  string targetDirectory,
                                  OccurrenceList occurrenceList,
                                  UniqueModuleEvaluator uniqueModuleEvaluator)
        {
            // TODO Test for the existance of folders and assemblies.
            TemplateAssemblyPath = templateAssemblyPath;
            UniqueModules        = uniqueModuleEvaluator;


            //Get the folder name that will be used to store the files associated with this Module.
            string folderName = GetModuleFolderPath();

            //Need to get number of the parent occ, top level name as foldername
            ModulePath = System.IO.Path.Combine(targetDirectory, folderName);

            string topFileFullName = occurrenceList.TargetAssembly.FullDocumentName;
            string topFileNameOnly = System.IO.Path.GetFileName(topFileFullName);

            ModuleAssemblyPath = System.IO.Path.Combine(ModulePath, topFileNameOnly);

            //If this file already exists in the current location, for now we are
            //going to just skip the file creation, and assume it was previously done
            //correctly.  Probably need to give the user the option to redo and
            //overwrite files if they want to.
            if (!System.IO.File.Exists(ModuleAssemblyPath))
            {
                FilePathPair = new TupleList <string, string>();

                for (int i = 0; i < occurrenceList.Items.Count; i++)
                {
                    string targetOccPath   = occurrenceList.Items[i].ReferencedFileDescriptor.FullFileName;
                    string newCopyName     = System.IO.Path.GetFileName(targetOccPath);
                    string newFullCopyName = System.IO.Path.Combine(ModulePath, newCopyName);
                    FilePathPair.Add(targetOccPath, newFullCopyName);
                }

                //Check if an earlier module already made the folder, if not, create it.
                if (!System.IO.Directory.Exists(ModulePath))
                {
                    //This property is needed later when placing occurrences of the assembly this Module instance
                    //refers to.  If FirstTime is false, we will want to have a slightly different strategry for constraint
                    //placement.  If FirstTime is true, all constraints are important and we need not relax them.  If
                    //FirstTime is false, then we need tolerance in the constraints because of double precision.  When
                    //FirstTime is false, we really just want to position the occurrence correctly, not drive its
                    //geometry.

                    if (FirstTime == null)
                    {
                        FirstTime = true;
                    }

                    System.IO.Directory.CreateDirectory(ModulePath);
                    ReplaceReferences(occurrenceList.TargetAssembly, FilePathPair, ModulePath);
                    AssemblyDocument oAssDoc = (AssemblyDocument)PersistenceManager.InventorApplication.Documents.Open(TemplateAssemblyPath, false);
                    oAssDoc.SaveAs(ModuleAssemblyPath, true);
                    oAssDoc.Close(true);


                    //Need to copy presentation files if there are any.  For now this is only going to work with the top assembly.
                    string   templateDirectory = System.IO.Path.GetDirectoryName(TemplateAssemblyPath);
                    string[] presentationFiles = System.IO.Directory.GetFiles(templateDirectory, "*.ipn");
                    //If we want the ability to have subassemblies with .ipn files or multiple ones, this will have to be changed
                    //to iterate over all the .ipn files.
                    if (presentationFiles.Length != 0)
                    {
                        string newCopyPresName     = System.IO.Path.GetFileName(presentationFiles[0]);
                        string newFullCopyPresName = System.IO.Path.Combine(ModulePath, newCopyPresName);
                        PresentationDocument          presentationDocument = (PresentationDocument)PersistenceManager.InventorApplication.Documents.Open(presentationFiles[0], false);
                        DocumentDescriptorsEnumerator presFileDescriptors  = presentationDocument.ReferencedDocumentDescriptors;
                        foreach (DocumentDescriptor refPresDocDescriptor in presFileDescriptors)
                        {
                            if (refPresDocDescriptor.FullDocumentName == TemplateAssemblyPath)
                            {
                                refPresDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModuleAssemblyPath);
                                presentationDocument.SaveAs(newFullCopyPresName, true);
                                presentationDocument.Close(true);
                            }
                        }
                    }
                }

                else
                {
                    FirstTime = false;
                }
            }
        }
Beispiel #4
0
        //TODO: MakeInvCopy is going to be called over and over again by DesignScript, not us, so there is no opportunity to pass the count into this method.
        //UniqueModuleEvaluator needs to be modified each time this is called so we know which module we are on.

        //TODO:  ApprenticeServer instance creation and lifetime management needs to be handled by InventorServices.Persistance

        //TODO: OccurrenceList needs to be set on each Module instance during UniqueModuleEvaluator's construction.

        //TODO: Refactor this method, it is so big.
        private void MakeInvCopy(ApprenticeServer appServ,
                                 string templateAssemblyPath,
                                 string templateDrawingPath,
                                 string targetDirectory,
                                 OccurrenceList occList,
                                 int count,
                                 UniqueModuleEvaluator uniqueModuleEvaluator)
        {
            // TODO Test for the existance of folders and assemblies.
            ApprenticeServer oAppServ = appServ;
            int            panelID    = count;
            OccurrenceList oOccs      = occList;
            string         topFileFullName;
            string         targetPath = targetDirectory;

            TemplateAssemblyPath = templateAssemblyPath;
            TemplateDrawingPath  = templateDrawingPath;
            string panelIDString = System.Convert.ToString(panelID);

            UniqueModules = uniqueModuleEvaluator;

            //Instead of using "panelID" to create unique folders for all instances, redirect to the GeometryMapIndex
            string geoMapString = System.Convert.ToString(GeometryMapIndex);
            string folderName;

            if (CreateAllCopies == false)
            {
                if (GeometryMapIndex < 10)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 00" + geoMapString;
                }

                else if (10 <= GeometryMapIndex && GeometryMapIndex < 100)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 0" + geoMapString;
                }
                else
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " " + geoMapString;
                }
            }

            else
            {
                if (panelID < 10)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 00" + panelIDString;
                }
                else if (10 <= panelID && panelID < 100)
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " 0" + panelIDString;
                }
                else
                {
                    folderName = System.IO.Path.GetFileNameWithoutExtension(TemplateAssemblyPath) + " " + panelIDString;
                }
            }
            //if(panelID < 10){
            //Need to get number of the parent occ, top level name as foldername
            string pathString = System.IO.Path.Combine(targetPath, folderName);

            topFileFullName = oOccs.TargetAssembly.FullDocumentName;
            string topFileNameOnly = System.IO.Path.GetFileName(topFileFullName);

            ModulePath = System.IO.Path.Combine(pathString, topFileNameOnly);


            TupleList <string, string> filePathPair = new TupleList <string, string>();

            for (int i = 0; i < occList.Items.Count; i++)
            {
                string targetOccPath   = occList.Items[i].ReferencedFileDescriptor.FullFileName;
                string newCopyName     = System.IO.Path.GetFileName(targetOccPath);
                string newFullCopyName = System.IO.Path.Combine(pathString, newCopyName);
                filePathPair.Add(targetOccPath, newFullCopyName);
            }

            //Check if an earlier panel already made the folder, if not, create it.
            if (!System.IO.Directory.Exists(pathString))
            {
                firstTime = true;
                System.IO.Directory.CreateDirectory(pathString);
                //AssemblyReplaceRef(oAppServ, oOccs.TargetAssembly, filePathPair, pathString);
                ApprenticeServerDocument oAssDoc;
                oAssDoc = oAppServ.Open(TemplateAssemblyPath);
                FileSaveAs fileSaver;
                fileSaver = oAppServ.FileSaveAs;
                fileSaver.AddFileToSave(oAssDoc, ModulePath);
                fileSaver.ExecuteSaveCopyAs();

                //Need to copy presentation files if there are any.  For now this is only going to work with the top assembly.
                string   templateDirectory = System.IO.Path.GetDirectoryName(TemplateAssemblyPath);
                string[] presentationFiles = System.IO.Directory.GetFiles(templateDirectory, "*.ipn");
                //If we want the ability to have subassemblies with .ipn files or multiple ones, this will have to be changed
                //to iterate over all the .ipn files.
                if (presentationFiles.Length != 0)
                {
                    string newCopyPresName     = System.IO.Path.GetFileName(presentationFiles[0]);
                    string newFullCopyPresName = System.IO.Path.Combine(pathString, newCopyPresName);

                    ApprenticeServerDocument      presentationDocument = oAppServ.Open(presentationFiles[0]);
                    DocumentDescriptorsEnumerator presFileDescriptors  = presentationDocument.ReferencedDocumentDescriptors;
                    foreach (DocumentDescriptor refPresDocDescriptor in presFileDescriptors)
                    {
                        if (refPresDocDescriptor.FullDocumentName == TemplateAssemblyPath)
                        {
                            refPresDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModulePath);
                            FileSaveAs fileSavePres;
                            fileSavePres = oAppServ.FileSaveAs;
                            fileSavePres.AddFileToSave(presentationDocument, newFullCopyPresName);
                        }
                    }
                }

                string newCopyDrawingName     = System.IO.Path.GetFileName(TemplateDrawingPath);
                string newFullCopyDrawingName = System.IO.Path.Combine(pathString, newCopyDrawingName);

                if (TemplateDrawingPath != "")
                {
                    ApprenticeServerDocument      drawingDoc             = oAppServ.Open(TemplateDrawingPath);
                    DocumentDescriptorsEnumerator drawingFileDescriptors = drawingDoc.ReferencedDocumentDescriptors;
                    //This needs to be fixed.  It was written with the assumption that only the template assembly would be in
                    //the details and be first in the collection of document descriptors.  Need to iterate through
                    //drawingFileDescriptors and match names and replace correct references.
                    //Possibly can use the "filePathPair" object for name matching/reference replacing.
                    //drawingFileDescriptors[1].ReferencedFileDescriptor.ReplaceReference(topAssemblyNewLocation);
                    foreach (DocumentDescriptor refDocDescriptor in drawingFileDescriptors)
                    {
                        foreach (Tuple <string, string> pathPair in filePathPair)
                        {
                            string newFileNameLower      = System.IO.Path.GetFileName(pathPair.Item2);
                            string drawingReferenceLower = System.IO.Path.GetFileName(refDocDescriptor.FullDocumentName);
                            string topAssemblyLower      = System.IO.Path.GetFileName(ModulePath);
                            if (topAssemblyLower == drawingReferenceLower)
                            {
                                refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModulePath);
                            }
                            if (newFileNameLower == drawingReferenceLower)
                            {
                                refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(pathPair.Item2);
                            }
                        }
                    }

                    FileSaveAs fileSaveDrawing;
                    fileSaveDrawing = oAppServ.FileSaveAs;
                    fileSaveDrawing.AddFileToSave(drawingDoc, newFullCopyDrawingName);
                    fileSaveDrawing.ExecuteSaveCopyAs();
                    firstTime = true;

                    if (!UniqueModules.DetailDocumentPaths.Contains(newFullCopyDrawingName))
                    {
                        UniqueModules.DetailDocumentPaths.Add(newFullCopyDrawingName);
                    }
                }
            }
        }
Beispiel #5
0
        public void ReplaceReferences(AssemblyDocument targetAssembly, TupleList<string, string> namePair, string folderPath)
        {
            OccurrenceList newOccs = new OccurrenceList(targetAssembly);
            string pathString = folderPath;
            List<string> patternComponentsList = new List<string>();
            for (int i = 0; i < newOccs.Items.Count; i++)
            {
                if (newOccs.Items[i].DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject)
                {
                    for (int f = 0; f < namePair.Count; f++)
                    {
                        if (namePair[f].Item1 == newOccs.Items[i].ReferencedFileDescriptor.FullFileName)
                        {
                            if (patternComponentsList.Contains(namePair[f].Item1))
                            {
                                newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[f].Item2);
                            }

                            else
                            {
                                if (!System.IO.File.Exists(namePair[f].Item2))
                                {
                                    PartDocument partDoc = (PartDocument)PersistenceManager.InventorApplication.Documents.Open(namePair[f].Item1, false);
                                    partDoc.SaveAs(namePair[f].Item2, true);
                                    partDoc.Close(true);
                                    newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[f].Item2);
                                }
                                patternComponentsList.Add(namePair[f].Item1);
                            }
                        }
                    }
                }

                else if (newOccs.Items[i].DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    for (int n = 0; n < namePair.Count; n++)
                    {
                        if (namePair[n].Item1 == newOccs.Items[i].ReferencedFileDescriptor.FullFileName)
                        {
                            AssemblyDocument subAssembly = (AssemblyDocument)PersistenceManager.InventorApplication.Documents.Open(newOccs.Items[i].ReferencedFileDescriptor.FullFileName,false);
                            ReplaceReferences(subAssembly, namePair, pathString);
                            string newFilePath = namePair[n].Item2;
                            subAssembly.SaveAs(namePair[n].Item2, true);
                            subAssembly.Close(true);
                            newOccs.Items[i].ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(namePair[n].Item2);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        internal void MakeInvCopy(string templateAssemblyPath,
                         string targetDirectory,
                         OccurrenceList occurrenceList,
                         UniqueModuleEvaluator uniqueModuleEvaluator)
        {
            // TODO Test for the existance of folders and assemblies.
            TemplateAssemblyPath = templateAssemblyPath;        
            UniqueModules = uniqueModuleEvaluator;

            
            //Get the folder name that will be used to store the files associated with this Module.
            string folderName = GetModuleFolderPath();

            //Need to get number of the parent occ, top level name as foldername
            ModulePath = System.IO.Path.Combine(targetDirectory, folderName);

            string topFileFullName = occurrenceList.TargetAssembly.FullDocumentName;
            string topFileNameOnly = System.IO.Path.GetFileName(topFileFullName);
            ModuleAssemblyPath = System.IO.Path.Combine(ModulePath, topFileNameOnly);

            //If this file already exists in the current location, for now we are
            //going to just skip the file creation, and assume it was previously done
            //correctly.  Probably need to give the user the option to redo and 
            //overwrite files if they want to.
            if (!System.IO.File.Exists(ModuleAssemblyPath))
            {
                FilePathPair = new TupleList<string, string>();

                for (int i = 0; i < occurrenceList.Items.Count; i++)
                {
                    string targetOccPath = occurrenceList.Items[i].ReferencedFileDescriptor.FullFileName;
                    string newCopyName = System.IO.Path.GetFileName(targetOccPath);
                    string newFullCopyName = System.IO.Path.Combine(ModulePath, newCopyName);
                    FilePathPair.Add(targetOccPath, newFullCopyName);
                }

                //Check if an earlier module already made the folder, if not, create it.
                if (!System.IO.Directory.Exists(ModulePath))
                {
                    //This property is needed later when placing occurrences of the assembly this Module instance 
                    //refers to.  If FirstTime is false, we will want to have a slightly different strategry for constraint
                    //placement.  If FirstTime is true, all constraints are important and we need not relax them.  If 
                    //FirstTime is false, then we need tolerance in the constraints because of double precision.  When
                    //FirstTime is false, we really just want to position the occurrence correctly, not drive its 
                    //geometry.

                    if (FirstTime == null)
                    {
                        FirstTime = true;
                    }

                    System.IO.Directory.CreateDirectory(ModulePath);
                    ReplaceReferences(occurrenceList.TargetAssembly, FilePathPair, ModulePath);
                    AssemblyDocument oAssDoc = (AssemblyDocument)PersistenceManager.InventorApplication.Documents.Open(TemplateAssemblyPath, false);
                    oAssDoc.SaveAs(ModuleAssemblyPath, true);
                    oAssDoc.Close(true);


                    //Need to copy presentation files if there are any.  For now this is only going to work with the top assembly.
                    string templateDirectory = System.IO.Path.GetDirectoryName(TemplateAssemblyPath);
                    string[] presentationFiles = System.IO.Directory.GetFiles(templateDirectory, "*.ipn");
                    //If we want the ability to have subassemblies with .ipn files or multiple ones, this will have to be changed
                    //to iterate over all the .ipn files.
                    if (presentationFiles.Length != 0)
                    {
                        string newCopyPresName = System.IO.Path.GetFileName(presentationFiles[0]);
                        string newFullCopyPresName = System.IO.Path.Combine(ModulePath, newCopyPresName);
                        PresentationDocument presentationDocument = (PresentationDocument)PersistenceManager.InventorApplication.Documents.Open(presentationFiles[0], false);
                        DocumentDescriptorsEnumerator presFileDescriptors = presentationDocument.ReferencedDocumentDescriptors;
                        foreach (DocumentDescriptor refPresDocDescriptor in presFileDescriptors)
                        {
                            if (refPresDocDescriptor.FullDocumentName == TemplateAssemblyPath)
                            {
                                refPresDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModuleAssemblyPath);
                                presentationDocument.SaveAs(newFullCopyPresName, true);
                                presentationDocument.Close(true);
                            }
                        }
                    }
                }

                else
                {
                    FirstTime = false;
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Main program control for copying, placing, constraining template assemblies,
        /// as well as evaluating duplicate geometries.
        /// </summary>
        /// <param name="templateAssemblyPath">Path to template assembly file to be copied and placed in model</param>
        /// <param name="destinationFolder">Path where created files will be saved</param>
        /// <param name="reuseDuplicates">Let the library determine if files can be reused</param>
        /// <param name="testMode">If true will create only three modules</param>
        private void InternalPlaceModules(string templateAssemblyPath, string destinationFolder, bool reuseDuplicates, bool testMode)
        {
            //Do some initial validation that this is going to work.
            //TODO: MOVE THIS INTO THE CONSTRUCTOR
            if (!IsConstraintSetUniform)
            {
                throw new Exception("Each module must have the same number of points.");
            }

            //If the user has chosen to try to reuse files for duplicate geometries, we need
            //evaluate that first before creating any new files.

            //TODO: MOVE THIS TO THE CONSTRUCTOR/ModulePoints property changed event.
            //Create this thing in constructor and set it to null in
            //this method if the user sets reuseDuplicates to false.  We
            //are evaluating this shit every time
            UniqueModules = null;

            if (reuseDuplicates)
            {
                ModulesList.Select(p => { p.ReuseDuplicates = false; return(p); }).ToList();
                //UniqueModuleEvaluator needs to be passed into the constructor.
                UniqueModules = UniqueModuleEvaluator.ByModules(ModulesList);
            }

            //We need to get a flattened list of all the ComponentOccurrence objects in the
            //template assembly.

            //Inventor's API was developed in Russia.
            AssemblyDocument templateAssembly    = (AssemblyDocument)PersistenceManager.InventorApplication.Documents.Open(templateAssemblyPath, false);
            OccurrenceList   templateOccurrences = new OccurrenceList(templateAssembly);

            _testMode = testMode;
            if (testMode)
            {
                if (ModulesList.Count < 3)
                {
                    for (int i = 0; i < ModulesList.Count; i++)
                    {
                        ModulesList[i].MakeInvCopy(templateAssemblyPath, destinationFolder, templateOccurrences, UniqueModules);
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        ModulesList[i].MakeInvCopy(templateAssemblyPath, destinationFolder, templateOccurrences, UniqueModules);
                    }
                }
            }
            else
            {
                for (int i = 0; i < ModulesList.Count; i++)
                {
                    ModulesList[i].MakeInvCopy(templateAssemblyPath, destinationFolder, templateOccurrences, UniqueModules);
                }
            }

            templateAssembly.Close();

            //ApprenticeServerComponent apprenticeServer = InventorPersistenceManager.ActiveApprenticeServer;
            //ApprenticeServerDocument templateAssemblyApprenticeDoc = apprenticeServer.Open(templateAssemblyPath);
            //OccurrenceList templateOccurrences = new OccurrenceList(apprenticeServer, templateAssemblyApprenticeDoc);
            //apprenticeServer.Close();


            //Create Inventor files needed to accommodate this set of "Modules".

            //Create a layout file.  This file will contain all the individual geometries as
            //work geometry.  It will be placed first in the assembly we are making, and each
            //Module will get constrained to its corresponding set of work geometry.
            //This only needs to happen the first time.
            EnsureLayoutPartExists(destinationFolder);

            //Place the layout part and put work geometry in it.
            CreateLayout(destinationFolder);

            //Place the actual Inventor assemblies.

            //PersistenceManager.InventorApplication.Visible = false;
            try
            {
                if (testMode)
                {
                    if (ModulesList.Count < 3)
                    {
                        for (int i = 0; i < ModulesList.Count; i++)
                        {
                            ModulesList[i].PlaceModule();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            ModulesList[i].PlaceModule();
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < ModulesList.Count; i++)
                    {
                        ModulesList[i].PlaceModule();
                    }
                }
            }
            catch
            {
                PersistenceManager.InventorApplication.Visible = true;
            }
            //PersistenceManager.InventorApplication.Visible = true;

            //Update the view
            PersistenceManager.ActiveAssemblyDoc.Update2();
        }