Example #1
0
        public List <Tuple <string, string, string, object> > GetProperties(string FileName)
        {
            SwDMDocument         swDoc    = default(SwDMDocument);
            SwDMConfigurationMgr swCfgMgr = default(SwDMConfigurationMgr);

            // config name
            // property name
            // property type
            // resolved value (boxed object)
            List <Tuple <string, string, string, object> > lstProps = new List <Tuple <string, string, string, object> >();

            // get doc type
            SwDmDocumentType swDocType = GetTypeFromString(FileName);

            if (swDocType == SwDmDocumentType.swDmDocumentUnknown)
            {
                return(null);
            }

            // get the document
            SwDmDocumentOpenError nRetVal = 0;

            swDoc = (SwDMDocument)swDocMgr.GetDocument(FileName, swDocType, true, out nRetVal);
            if (SwDmDocumentOpenError.swDmDocumentOpenErrorNone != nRetVal)
            {
                DialogResult dr = MessageBox.Show("Failed to open solidworks file: " + FileName,
                                                  "Loading SW File",
                                                  MessageBoxButtons.OK,
                                                  MessageBoxIcon.Exclamation,
                                                  MessageBoxDefaultButton.Button1);
            }

            // get document custom properties (file level properties)
            string[] strDocPropNames = (string[])swDoc.GetCustomPropertyNames();
            if (strDocPropNames != null)
            {
                foreach (string strPropName in strDocPropNames)
                {
                    SwDmCustomInfoType nPropType  = 0;
                    object             oPropValue = swDoc.GetCustomProperty(strPropName, out nPropType);

                    // property type
                    string strPropType = "";
                    switch (nPropType)
                    {
                    case SwDmCustomInfoType.swDmCustomInfoDate:
                        strPropType = "date";
                        oPropValue  = Convert.ToDateTime(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoNumber:
                        strPropType = "number";
                        oPropValue  = Convert.ToDecimal(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoText:
                        strPropType = "text";
                        oPropValue  = Convert.ToString(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoYesOrNo:
                        strPropType = "yesno";
                        oPropValue  = oPropValue.Equals("Yes");
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoUnknown:
                        strPropType = "";
                        break;
                    }

                    // add to list
                    lstProps.Add(Tuple.Create <string, string, string, object>("", strPropName, strPropType, oPropValue));
                }
            }

            // drawings don't have configurations, so we can return here
            if (swDocType == SwDmDocumentType.swDmDocumentDrawing)
            {
                return(lstProps);
            }

            // parts and assemblies have configurations
            // get a list of configs
            List <string> lstConfigNames;

            swCfgMgr       = swDoc.ConfigurationManager;
            lstConfigNames = new List <string>((string[])swCfgMgr.GetConfigurationNames());

            // get properties
            foreach (string strConfigName in lstConfigNames)
            {
                SwDMConfiguration swCfg           = (SwDMConfiguration)swCfgMgr.GetConfigurationByName(strConfigName);
                string[]          strCfgPropNames = swCfg.GetCustomPropertyNames();
                if (strCfgPropNames == null)
                {
                    continue;
                }

                foreach (string strPropName in strCfgPropNames)
                {
                    SwDmCustomInfoType nPropType  = 0;
                    object             oPropValue = swCfg.GetCustomProperty(strPropName, out nPropType);

                    // property type
                    string strPropType = "";
                    switch (nPropType)
                    {
                    case SwDmCustomInfoType.swDmCustomInfoDate:
                        strPropType = "date";
                        oPropValue  = Convert.ToDateTime(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoNumber:
                        strPropType = "number";
                        oPropValue  = Convert.ToDecimal(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoText:
                        strPropType = "text";
                        oPropValue  = Convert.ToString(oPropValue);
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoYesOrNo:
                        strPropType = "yesno";
                        oPropValue  = oPropValue.Equals("Yes");
                        break;

                    case SwDmCustomInfoType.swDmCustomInfoUnknown:
                        strPropType = "";
                        break;
                    }

                    // add to list
                    lstProps.Add(Tuple.Create <string, string, string, object>(strConfigName, strPropName, strPropType, oPropValue));
                }
            }

            swDoc.CloseDoc();
            return(lstProps);
        }
        protected bool TraverseCore(string assemblyPath, string configurationName)
        {
            if (string.IsNullOrEmpty(assemblyPath))
            {
                return(false);
            }
            if (!File.Exists(assemblyPath))
            {
                return(false);
            }
            string           originalExt;
            SwDmDocumentType docType = SldFileExtentionChecker.CheckDM(assemblyPath, out originalExt);

            if (docType != SwDmDocumentType.swDmDocumentAssembly && docType != SwDmDocumentType.swDmDocumentPart)
            {
                return(false);
            }
            SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
            //this.AllComObjects.Enqueue(swDMClassFactory);
            SwDMApplication swDMApp = swDMClassFactory.GetApplication(GetDocumentPropertiesViaDM.LinktronLicenseKey);

            this.GlobalSearchOption = swDMApp.GetSearchOptionObject();
            //this.AllComObjects.Enqueue(swDMApp);
            SwDmDocumentOpenError returnValue = 0;
            SwDMDocument17        swDoc       = (SwDMDocument17)swDMApp.GetDocument(assemblyPath, docType, true, out returnValue);

            if (swDoc == null || returnValue != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
            {
                return(false);
            }
            //this.AllComObjects.Enqueue(swDoc);
            SwDMConfigurationMgr dmConfigMgr = swDoc.ConfigurationManager;

            //this.AllComObjects.Enqueue(dmConfigMgr);
            string[] configurationNames = (string[])dmConfigMgr.GetConfigurationNames();
            if (configurationNames == null || configurationNames.Length <= 0)
            {
                return(false);
            }
            string configNameToOpen = null;

            if (string.IsNullOrEmpty(configurationName))
            {
                configNameToOpen = dmConfigMgr.GetActiveConfigurationName();
            }
            else
            {
                configNameToOpen = configurationName;
            }
            SwDMConfiguration14 activeCfg = (SwDMConfiguration14)dmConfigMgr.GetConfigurationByName(configNameToOpen);

            if (activeCfg == null)
            {
                return(false);
            }
            //this.AllComObjects.Enqueue(activeCfg);
            int topId = this.GetTopDocumentInfo(assemblyPath, swDoc, activeCfg);

            if (topId <= 0)
            {
                return(false);
            }

            if (docType == SwDmDocumentType.swDmDocumentAssembly)
            {
                try
                {
                    object[] allComponents = activeCfg.GetComponents();
                    if (allComponents != null)
                    {
                        foreach (object o in allComponents)
                        {
                            SwDMComponent9 subComponent = o as SwDMComponent9;
                            this.TraverseRecursively(subComponent, 1, topId);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(string.Format("TraverseCore:exception:{0}", ex.Message));
                }
            }
            return(true);
        }