Exemple #1
0
        public void RunWithArguments(Document doc, NameValueMap map)
        {
            if (doc == null && map.HasKey("ilod"))
            {
                string docPath = map.AsString("ilod");
                LogTrace($"Opening /ilod document name: {docPath}");

                if (docPath.ToUpper().EndsWith(".IAM"))
                {
                    FileManager fm = _inventorApplication.FileManager;

                    string dvActRep = fm.GetLastActiveDesignViewRepresentation(docPath);
                    LogTrace($"LastActiveDesignViewRepresentation: {dvActRep}");

                    string lodActRep = fm.GetLastActiveLevelOfDetailRepresentation(docPath);
                    LogTrace($"LastActiveLevelOfDetailRepresentation: {lodActRep}");

                    NameValueMap openOptions = _inventorApplication.TransientObjects.CreateNameValueMap();
                    openOptions.Add("LevelOfDetailRepresentation", lodActRep);
                    openOptions.Add("DesignViewRepresentation", dvActRep);

                    doc = _inventorApplication.Documents.OpenWithOptions(docPath, openOptions, false);
                }
                else
                {
                    doc = _inventorApplication.Documents.Open(docPath, false);
                }
                LogTrace($"Full document name: {doc.FullDocumentName}");
            }

            ExecWithArguments(doc, map);
        }
        public void StringTest()
        {
            var expectedResult = "TestString";

            nameValueMap.Value["StringValue"] = "TestString";


            string str1 = nameValueMap.AsString("StringValue");

            Assert.Equal(expectedResult, str1);
        }
        public override void ExecWithArguments(Document doc, NameValueMap map)
        {
            LogTrace($"ExecWithArguments called with {doc.DisplayName} with {map.Count} arguments");

            using (new HeartBeat())
            {
                Parameters parameters;
                switch (doc.DocumentType)
                {
                case DocumentTypeEnum.kPartDocumentObject:
                    parameters = ((PartDocument)doc).ComponentDefinition.Parameters;
                    break;

                case DocumentTypeEnum.kAssemblyDocumentObject:
                    parameters = ((AssemblyDocument)doc).ComponentDefinition.Parameters;
                    break;

                default:
                    LogError($"Unsupported document type: {doc.DocumentType}");
                    return;
                }

                LogTrace("Read parameters");

                string paramFile = map.AsString("paramFile");
                string json      = System.IO.File.ReadAllText(paramFile);
                LogTrace(json);

                var incomingParams = JsonConvert.DeserializeObject <InventorParameters>(json);

                foreach (var pair in incomingParams)
                {
                    string            paramName = pair.Key;
                    InventorParameter paramData = pair.Value;

                    try
                    {
                        var userParameter = parameters[paramName];

                        string expression = userParameter.Expression;
                        if (!paramData.Value.Equals(expression))
                        {
                            LogTrace($"Applying '{paramData.Value}' to '{paramName}'");
                            SetExpression(userParameter, paramData, doc);
                            if (paramData.ErrorMessage != null)
                            {
                                // there was an invalid expression the UI will prompt the user to fix, so don't bother changing the rest of the parameters
                                LogTrace("Skip update for the remaining parameters");
                                break;
                            }
                        }
                        else
                        {
                            LogTrace($"Skipping '{paramName}'");
                        }
                    }
                    catch (Exception e)
                    {
                        // some parameters (maybe when parameter gets driven by iLogic rule) are throwing error on change.
                        // so swallow the thrown error, log about it and continue
                        LogError($"Failed to set '{paramName}' parameter. Error is {e}.");
                    }
                }

                var paramsExtractor = new ParametersExtractor();
                paramsExtractor.Extract(doc, parameters, incomingParams);
            }
        }