Ejemplo n.º 1
0
        public stdole.IPictureDisp GetPreview2(string FileName, bool Deep = false)
        {
            // This method is only supported for in-process execution.  We need this the execute in a multi-threaded environment.
            // I tried starting a new instance of the SWDocMgr class for each thread, and
            // that seemed to work once, then didn't work, no explanation.
            // The error, when in a thread, is "Catastrophic Failure."
            // Use GetPreview() instead

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

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

            // get the document
            SwDMDocument19        swDoc;
            SwDmDocumentOpenError nRetVal = 0;

            swDoc = (SwDMDocument19)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);
                return(null);
            }

            SwDmPreviewError ePrevError = SwDmPreviewError.swDmPreviewErrorNone;

            stdole.IPictureDisp ipicPreview;
            try
            {
                ipicPreview = swDoc.GetPreviewPNGBitmap(out ePrevError);
                swDoc.CloseDoc();
                return(ipicPreview);
            }
            catch
            {
                //DialogResult dr = MessageBox.Show("Failed to get solidworks preview image: " + FileName + ": " + ePrevError.ToString(),
                //    "Loading SW Preview",
                //    MessageBoxButtons.OK,
                //    MessageBoxIcon.Exclamation,
                //    MessageBoxDefaultButton.Button1);
                swDoc.CloseDoc();
                return(null);
            }
        }
Ejemplo n.º 2
0
        public Bitmap GetPreview(string FileName, bool Deep = false)
        {
            // external references for assembly files (GetAllExternalReferences4)
            // external references for part files (GetExternalFeatureReferences)
            SwDMDocument11 swDoc = default(SwDMDocument11);

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

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

            // get the document
            SwDmDocumentOpenError nRetVal = 0;

            swDoc = (SwDMDocument11)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);
                return(null);
            }

            SwDmPreviewError ePrevError = SwDmPreviewError.swDmPreviewErrorNone;

            try
            {
                byte[]       bPreview = swDoc.GetPreviewBitmapBytes(out ePrevError);
                MemoryStream ms       = new MemoryStream(bPreview);
                Bitmap       bmp      = (Bitmap)Bitmap.FromStream(ms);

                // crop and pad the image to 640x480
                Bitmap bmp2 = resizeImage(bmp);
                return(bmp2);
            }
            catch
            {
                //DialogResult dr = MessageBox.Show("Failed to get solidworks preview image: " + FileName + ": " + ePrevError.ToString(),
                //    "Loading SW Preview",
                //    MessageBoxButtons.OK,
                //    MessageBoxIcon.Exclamation,
                //    MessageBoxDefaultButton.Button1);
                return(null);
            }
        }
Ejemplo n.º 3
0
        public static void ActualizaAllCash()
        {
            string[] notHiddenDirectories = GetNotHiddenDirectories(Furniture.Helpers.LocalAccounts.modelPathResult);
            string[] filesInCurrentDir = new string[0];

            SwDmDocumentOpenError oe = new SwDmDocumentOpenError();
            SwDMApplication swDocMgr = SwAddin.GetSwDmApp();
            SwDMSearchOption src = swDocMgr.GetSearchOptionObject(); ;
            SwDmCustomInfoType type = new SwDmCustomInfoType();

            foreach (var notHiddenDirectory in notHiddenDirectories)
            {
                string rFileName = notHiddenDirectory.Replace("_SWLIB_", "_SWLIB_BACKUP").Replace("_swlib_", "_swlib_backup");
                filesInCurrentDir = Directory.GetFiles(rFileName, "*.SLDASM");
                foreach (var file in filesInCurrentDir)
                {
                    rFileName = file;
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(rFileName);
                    if (rFileName.ToLower().Contains("_swlib_backup") && (fileNameWithoutExtension.Last() == 'P' || fileNameWithoutExtension.Last() == 'p') && Path.GetExtension(rFileName) == ".SLDASM" && !rFileName.ToLower().Contains("вспомогательн"))
                    {

                        int rr;
                        if (int.TryParse(fileNameWithoutExtension.Substring(fileNameWithoutExtension.Length - 3, 2), out rr))
                        {
                            //if (rr != 1)
                            //{
                            //    string newFilePath = Path.Combine(Path.GetDirectoryName(rFileName),
                            //                                      Path.GetFileNameWithoutExtension(rFileName).Substring(
                            //                                          0,
                            //                                          Path.GetFileNameWithoutExtension(rFileName).Length - 4) +
                            //                                      "#01P.SLDASM"); // + Path.GetExtension(fileName));
                            //    if (File.Exists(newFilePath))
                            //        File.SetAttributes(newFilePath, FileAttributes.Normal);
                            //    File.SetAttributes(file, FileAttributes.Hidden);
                            //}

                            if ((File.GetAttributes(file) & FileAttributes.Hidden) == FileAttributes.Hidden)
                            {
                                DeleteIfNonAccessory(file, src, swDocMgr, oe, type);

                            }
                        }
                    }
                }

            }
        }
Ejemplo n.º 4
0
        private static void DeleteIfNonAccessory(string file, SwDMSearchOption src, SwDMApplication swDocMgr, SwDmDocumentOpenError oe, SwDmCustomInfoType type)
        {
            //удалить все связанные, кроме аксесуаров..
            if (!File.Exists(file))
                return;
            if (!file.ToLower().Contains("_swlib_backup") || Path.GetFileNameWithoutExtension(file).First() == '~')
                return;
            var swDoc = swDocMgr.GetDocument(file, SwDmDocumentType.swDmDocumentAssembly, true, out oe);
            if (swDoc == null)
            {
                Logging.Log.Instance.Debug("Не удалось сделать GetDocument в " + file);
                return;
            }
            bool isAccessory;
            try
            {
                string[] tmp = swDoc.GetCustomPropertyNames();
                if (tmp != null && tmp.Contains("Accessories"))
                    isAccessory = swDoc.GetCustomProperty("Accessories", out type) == "Yes";
                else
                    isAccessory = false;
            }
            catch (Exception)
            {
                Logging.Log.Instance.Debug("Ошибка при попытке обратится к св-ву Accessories. Деталь: " +
                                            swDoc.FullName);
                swDoc.CloseDoc();
                return;
            }
            if (isAccessory) // если это аксесуар, то ничего не делаем.
            {
                swDoc.CloseDoc();
                return;
            }
            if (Path.GetExtension(file) == ".SLDASM")
            {
                string[] extReferences = swDoc.GetAllExternalReferences(src);
                swDoc.CloseDoc();
                foreach (var extReference in extReferences)
                {
                    DeleteIfNonAccessory(extReference, src, swDocMgr, oe, type);
                }
            }
            else
                swDoc.CloseDoc();

            if (Path.GetExtension(file) == ".SLDASM" || Path.GetExtension(file) == ".SLDPRT")
                File.Delete(file);
        }
Ejemplo n.º 5
0
        public List <string[]> GetDependencies(string FileName, bool Deep = false)
        {
            // external references for assembly files (GetAllExternalReferences4)
            // external references for part files (GetExternalFeatureReferences)
            SwDMDocument19   swDoc       = default(SwDMDocument19);
            SwDMSearchOption swSearchOpt = default(SwDMSearchOption);

            // returns list of string arrays
            // 0: short file name
            // 1: long file name
            List <string[]> listDepends = new List <string[]>();

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

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

            // get the document
            SwDmDocumentOpenError nRetVal = 0;

            swDoc = (SwDMDocument19)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);
                return(null);
            }

            // get arrays of dependency info (one-dimensional)
            object oBrokenRefVar;
            object oIsVirtual;
            object oTimeStamp;

            swSearchOpt = swDocMgr.GetSearchOptionObject();
            string[] varDepends = (string[])swDoc.GetAllExternalReferences4(swSearchOpt, out oBrokenRefVar, out oIsVirtual, out oTimeStamp);
            if (varDepends == null)
            {
                return(null);
            }

            Boolean[] blnIsVirtual = (Boolean[])oIsVirtual;
            for (int i = 0; i < varDepends.Length; i++)
            {
                // file name with absolute path
                string strFullName = varDepends[i];

                // short file name with extension
                string strName = strFullName.Substring(strFullName.LastIndexOf("\\") + 1);

                // only return non-virtual components
                if ((bool)blnIsVirtual[i] != true)
                {
                    string[] strDepend = new string[2] {
                        strName, strFullName
                    };
                    listDepends.Add(strDepend);
                }
            }

            swDoc.CloseDoc();
            return(listDepends);
        }
Ejemplo n.º 6
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);
        }