コード例 #1
0
        public void DesignViewSample()
        {
            AssemblyDocument            oDoc    = (AssemblyDocument)_InvApplication.ActiveDocument;
            AssemblyComponentDefinition oAsmDef = oDoc.ComponentDefinition;


            DesignViewRepresentations oDesignViewReps = oAsmDef.RepresentationsManager.DesignViewRepresentations;

            // Use ComponentOccurrence functionality to set the state, (visibility, color, etc.).
            // When the design view is created it will capture the current state of the assembly.

            DesignViewRepresentation oDesignViewRep = default(DesignViewRepresentation);

            oDesignViewRep = oDesignViewReps.Add("Test");

            // Activate the master design view.
            oDesignViewReps["Master"].Activate();
        }
コード例 #2
0
        public void RunWithArguments(Document doc, NameValueMap map)
        {
            // map["_1"] contains absolute path to configuration json
            // map["_2"] contains absolute path to folder with module STEP (or other types of) files
            // map["_3"] (optional) contains relative path to assembly to open

            /* Sample json structure
             * [
             *   {  "path": "some\\path\\to\\enclosure.iam",
             *      "xform": [
             *          1, 0, 0, 10,
             *          0, 1, 0,  0,
             *          0, 0, 1,  0,
             *          0, 0, 0,  1
             *      ]
             *   },
             *   {  "path": "some\\path\\to\\part.ipt",
             *      "xform": [
             *          1, 0, 0, 10,
             *          0, 1, 0, 20,
             *          0, 0, 1, 30,
             *          0, 0, 0,  1
             *      ]
             *    }
             * ]
             */

            try
            {
                using (new HeartBeat(30000))
                {
                    string currentDir = System.IO.Directory.GetCurrentDirectory();

                    LogTrace("Parsing configuration");
                    string             json   = System.IO.File.ReadAllText((string)map.Value["_1"]);
                    List <ConfigEntry> config = JsonConvert.DeserializeObject <List <ConfigEntry> >(json);

                    string modulesDir = (string)map.Value["_2"];

                    LogTrace("Assembling modules");
                    doc = inventorApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, "", false);

                    // Let's save it (and provide file name for it) befroe doing anything else with it
                    // If we tried to do it in the end with 2022 engine, we'd get an error
                    var assemblyPath = System.IO.Path.Combine(modulesDir, "template.iam");
                    doc.SaveAs(assemblyPath, false);
                    AssemblyDocument asmDoc = doc as AssemblyDocument;

                    // Add Design View representation in case we need it
                    DesignViewRepresentation dvr = asmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Add("Default");
                    dvr.Activate();

                    AssemblyComponentDefinition compDef = asmDoc.ComponentDefinition;
                    TransientGeometry           geom    = inventorApplication.TransientGeometry;
                    Matrix matrix;

                    var moduleOccurrences = new Dictionary <string, ComponentOccurrence>();
                    foreach (var entry in config)
                    {
                        LogTrace("Module {0} ({1})", entry.Path, String.Join(", ", entry.Xform));
                        try
                        {
                            matrix = geom.CreateMatrix();
                            matrix.PutMatrixData(entry.Xform);

                            ComponentOccurrence compOcc = null;
                            if (moduleOccurrences.ContainsKey(entry.Path))
                            {
                                compOcc = compDef.Occurrences.AddByComponentDefinition(moduleOccurrences[entry.Path].Definition, matrix);
                            }
                            else
                            {
                                var modulePath = System.IO.Path.Combine(modulesDir, entry.Path);

                                if (IsInventorDocument(modulePath))
                                {
                                    string dvRep  = null;
                                    string lodRep = null;
                                    if (IsAssemblyDocument(modulePath))
                                    {
                                        dvRep  = inventorApplication.FileManager.GetLastActiveDesignViewRepresentation(modulePath);
                                        lodRep = inventorApplication.FileManager.GetLastActiveLevelOfDetailRepresentation(modulePath);
                                    }

                                    compOcc = compDef.Occurrences.Add(modulePath, matrix);

                                    if (dvRep != null)
                                    {
                                        compOcc.SetDesignViewRepresentation(dvRep);
                                    }

                                    if (lodRep != null)
                                    {
                                        compOcc.SetLevelOfDetailRepresentation(lodRep);
                                    }
                                }
                                else
                                {
                                    // Create the ImportedGenericComponentDefinition bases on an Alias file
                                    LogTrace("Path of module: {0}", modulePath);
                                    dynamic oImportedGenericCompDef = compDef.ImportedComponents.CreateDefinition(modulePath);

                                    // Set the ReferenceModel to associatively import the Alias file
                                    oImportedGenericCompDef.ReferenceModel = true;

                                    // Import the Solidworks to assembly
                                    var oImportedComp = compDef.ImportedComponents.Add(oImportedGenericCompDef);
                                    // Not doing an Update afetr adding a SolidWorks assembly can cause problems
                                    asmDoc.Update2();

                                    compOcc = compDef.Occurrences[compDef.Occurrences.Count];
                                    compOcc.Transformation = matrix;
                                }

                                moduleOccurrences.Add(entry.Path, compOcc);
                            }

                            // Debugging
                            dynamic defDoc         = compOcc.Definition.Document;
                            var     occurrencePath = defDoc.FullFileName;
                            LogTrace("Path of added occurrence: {0}", occurrencePath);
                        }
                        catch (Exception e)
                        {
                            LogTrace("Cannot add file {0}: {1}", entry.Path, e.Message);
                        }
                    }

                    LogTrace("Finishing assembly");
                    doc.Update();

                    LogTrace("Saving assembly");
                    doc.Save2();
                    var rfaPath = System.IO.Path.Combine(currentDir, "output.rfa");
                    ExportRFA(doc, rfaPath);

                    /*
                     * LogTrace("Copying referenced documents to working folder");
                     * foreach(dynamic rf in doc.File.AllReferencedFiles)
                     * {
                     *  var oldPath = rf.FullFileName;
                     *  var docName = System.IO.Path.GetFileName(oldPath);
                     *  var newPath = System.IO.Path.Combine(documentDir, docName);
                     *  System.IO.File.Copy(oldPath, newPath, true);
                     * }
                     */

                    LogTrace("Compressing output");
                    using (var archive = ZipFile.Open("output.zip", ZipArchiveMode.Create))
                    {
                        archive.CreateEntryFromFile(assemblyPath, System.IO.Path.GetFileName(assemblyPath));

                        foreach (dynamic rf in doc.File.AllReferencedFiles)
                        {
                            string fileName = rf.FullFileName;
                            archive.CreateEntryFromFile(fileName, System.IO.Path.GetFileName(fileName));
                        }
                    }

                    LogTrace("Closing assembly");
                    doc.Close(true);

                    /*
                     * LogTrace("Compressing output");
                     * using (var archive = ZipFile.Open(System.IO.Path.Combine(documentDir, "output.zip"), ZipArchiveMode.Create))
                     * {
                     *  foreach (var entry in System.IO.Directory.EnumerateFiles(documentDir))
                     *  {
                     *      if (entry.EndsWith(".ipt") || entry.EndsWith(".iam"))
                     *      {
                     *          archive.CreateEntryFromFile(entry, System.IO.Path.GetFileName(entry));
                     *      }
                     *  }
                     * }
                     */
                }
            }
            catch (Exception e)
            {
                LogError("Processing failed. " + e.ToString());
            }
        }