internal void GenerateDrawings(string templateDrawingPath) { TemplateDrawingPath = templateDrawingPath; if (FirstTime == true && TemplateDrawingPath != null && TemplateDrawingPath != "") { string newCopyDrawingName = System.IO.Path.GetFileName(TemplateDrawingPath); string newFullCopyDrawingName = System.IO.Path.Combine(ModulePath, newCopyDrawingName); if (!System.IO.File.Exists(newFullCopyDrawingName)) { DrawingDocument drawingDoc = (DrawingDocument)PersistenceManager.InventorApplication.Documents.Open(TemplateDrawingPath, false); 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. This was a safe assumption when //I was the only user of this code. 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(ModuleAssemblyPath); if (topAssemblyLower == drawingReferenceLower) { refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(ModuleAssemblyPath); } if (newFileNameLower == drawingReferenceLower) { refDocDescriptor.ReferencedFileDescriptor.ReplaceReference(pathPair.Item2); } } } drawingDoc.SaveAs(newFullCopyDrawingName, false); drawingDoc.Close(true); if (!UniqueModules.DetailDocumentPaths.Contains(newFullCopyDrawingName)) { UniqueModules.DetailDocumentPaths.Add(newFullCopyDrawingName); } } } }
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; } } }
//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); } } } }