SwDmDocumentType GetTypeFromString(string ModelPathName) { // ModelPathName = fully qualified name of file SwDmDocumentType nDocType = 0; // Determine type of SOLIDWORKS file based on file extension if (ModelPathName.ToLower().EndsWith("sldprt")) { nDocType = SwDmDocumentType.swDmDocumentPart; } else if (ModelPathName.ToLower().EndsWith("sldasm")) { nDocType = SwDmDocumentType.swDmDocumentAssembly; } else if (ModelPathName.ToLower().EndsWith("slddrw")) { nDocType = SwDmDocumentType.swDmDocumentDrawing; } else { // Not a SOLIDWORKS file nDocType = SwDmDocumentType.swDmDocumentUnknown; } return(nDocType); }
void OpenDoc() { _swDocMgr = _swClassFact.GetApplication(SLicenseKey); _nDocType = SwDocMgrDocType(); _swDocument10 = (SwDMDocument10)_swDocMgr.GetDocument(SDocFileName, _nDocType, false, out _nRetVal); _swDocument13 = (SwDMDocument13)_swDocument10; }
public void SetProperty(string filePath, string configName, string fieldName, out string value, out string nPropTypeStr) { nPropTypeStr = ""; value = ""; //OpenDoc var swClassFact = new SwDMClassFactory(); var swDocMgr = swClassFact.GetApplication("82EC0E04A67C7A48A3AB757A947C507A8210C1E87738B58E"); //_nDocType = SwDocMgrDocType(filePath); const SwDmDocumentType nDocType = SwDmDocumentType.swDmDocumentPart; var swDocument15 = (SwDMDocument15)swDocMgr.GetDocument(@filePath, nDocType, true, out _nRetVal); var swDocument13 = (SwDMDocument13)swDocument15; var swCfgMgr = swDocument13.ConfigurationManager; var swCfg = (SwDMConfiguration13)swCfgMgr.GetConfigurationByName(configName); ProcessConfigCustomProperties(swCfg); try { SwDmCustomInfoType nPropType; value = swCfg.GetCustomProperty("Материал", out nPropType); swCfg.SetCustomProperty("Материал", "Лист Оцинковка"); MessageBox.Show(value); value = swCfg.GetCustomProperty("Материал", out nPropType); MessageBox.Show(value); nPropTypeStr = nPropType.ToString(); } catch (Exception e) { MessageBox.Show(e.Message); } // CloseDoc swDocument15.CloseDoc(); }
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); } }
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); } }
public override bool openDocument(FileObj file) { try { SwDmDocumentOpenError oError; swType = docTypeForExtension(file.ext); if (swType != SwDmDocumentType.swDmDocumentUnknown) { doc = (SwDMDocument10)app.GetDocument(file.path, swType, false, out oError); if (doc != null && (int)oError == (int)SwDmDocumentOpenError.swDmDocumentOpenErrorNone) return true; } } catch {} doc = null; return false; }
private SwDmDocumentType SwDocMgrDocType(string filePath) { if (filePath.Contains("sldprt")) { _nDocType = SwDmDocumentType.swDmDocumentPart; } else if (filePath.Contains("sldasm")) { _nDocType = SwDmDocumentType.swDmDocumentAssembly; } else if (filePath.Contains("slddrw")) { _nDocType = SwDmDocumentType.swDmDocumentDrawing; } else { _nDocType = SwDmDocumentType.swDmDocumentUnknown; } return _nDocType; }
private SwDmDocumentType SwDocMgrDocType(string filePath) { if (filePath.Contains("sldprt")) { _nDocType = SwDmDocumentType.swDmDocumentPart; } else if (filePath.Contains("sldasm")) { _nDocType = SwDmDocumentType.swDmDocumentAssembly; } else if (filePath.Contains("slddrw")) { _nDocType = SwDmDocumentType.swDmDocumentDrawing; } else { _nDocType = SwDmDocumentType.swDmDocumentUnknown; } return(_nDocType); }
public void GetProperty(string filePath, string configName, string fieldName, out string value, out string nPropTypeStr) { //OpenDoc var swClassFact = new SwDMClassFactory(); var swDocMgr = swClassFact.GetApplication("82EC0E04A67C7A48A3AB757A947C507A8210C1E87738B58E"); //_nDocType = SwDocMgrDocType(filePath); const SwDmDocumentType nDocType = SwDmDocumentType.swDmDocumentPart; var swDocument15 = (SwDMDocument15)swDocMgr.GetDocument(@filePath, nDocType, true, out _nRetVal); var swDocument13 = (SwDMDocument13)swDocument15; var swCfgMgr = swDocument13.ConfigurationManager; var swCfg = (SwDMConfiguration13)swCfgMgr.GetConfigurationByName(configName); SwDmCustomInfoType nPropType; value = swCfg.GetCustomProperty(fieldName, out nPropType); nPropTypeStr = nPropType.ToString(); // CloseDoc swDocument15.CloseDoc(); }
private SwDmDocumentType SwDocMgrDocType() { if (SDocFileName.ToLower().EndsWith("sldprt")) { _nDocType = SwDmDocumentType.swDmDocumentPart; } else if (SDocFileName.ToLower().EndsWith("sldasm")) { _nDocType = SwDmDocumentType.swDmDocumentAssembly; } else if (SDocFileName.ToLower().EndsWith("slddrw")) { _nDocType = SwDmDocumentType.swDmDocumentDrawing; } else { // Not a SolidWorks file _nDocType = SwDmDocumentType.swDmDocumentUnknown; } return(_nDocType); }
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); }
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); }
static void Main(string[] args) { //Takes Care of input checking and input parsing string docPath; bool quietMode; switch (args.Length) { case 1: quietMode = false; if (args[0].Contains("*") || args[0].Contains("?")) { inputError(quietMode); return; } docPath = Path.GetFullPath(args[0]); break; case 2: if (args[0] != "/q") { quietMode = false; inputError(quietMode); return; } quietMode = true; if (args[1].Contains("*") || args[1].Contains("?")) { inputError(quietMode); return; } docPath = Path.GetFullPath(args[1]); break; default: quietMode = false; inputError(quietMode); return; } //Get Document Type SwDmDocumentType docType = setDocType(docPath); if (docType == SwDmDocumentType.swDmDocumentUnknown) { inputError(quietMode); return; } //Variable initialization SwDMDocument15 dmDoc; SwDmDocumentOpenError OpenError; ////Prerequisites dmClassFact = new SwDMClassFactory(); dmDocManager = dmClassFact.GetApplication(SolidWorksDocumentManagerKey) as SwDMApplication3; //Open the Document dmDoc = dmDocManager.GetDocument(docPath, docType, true, out OpenError) as SwDMDocument15; //Check that a SolidWorks file is open if (dmDoc != null) { try { GetRefs(ref dmDoc); } catch (Exception e) { Console.WriteLine("\"" + dmDoc.FullName + "\"\t\"" + "File is internally damaged, .NET error occurred, or GetReferences.exe has a Bug. " + "Error Message: " + e.Message.Replace(Environment.NewLine, " ") + ". Stack Trace: " + e.StackTrace.Replace(Environment.NewLine, " ") + "\""); inputError(quietMode); } dmDoc.CloseDoc(); } else { switch (OpenError) { case SwDmDocumentOpenError.swDmDocumentOpenErrorFail: Console.WriteLine("\"" + docPath + "\"\t\"" + "File failed to open; reasons could be related to permissions, the file is in use, or the file is corrupted." + "\""); inputError(quietMode); break; case SwDmDocumentOpenError.swDmDocumentOpenErrorFileNotFound: Console.WriteLine("\"" + docPath + "\"\t\"" + "File not found or file path is longer than 255 characters." + "\""); inputError(quietMode); break; case SwDmDocumentOpenError.swDmDocumentOpenErrorNonSW: Console.Write("\"" + docPath + "\"\t\"" + "Non-SolidWorks file was opened" + "\""); inputError(quietMode); break; default: Console.WriteLine("\"" + docPath + "\"\t\"" + "An unknown errror occurred. Something is wrong with the code of GetReferences" + "\""); inputError(quietMode); break; } } }
private SwDmDocumentType SwDocMgrDocType() { if (SDocFileName.ToLower().EndsWith("sldprt")) { _nDocType = SwDmDocumentType.swDmDocumentPart; } else if (SDocFileName.ToLower().EndsWith("sldasm")) { _nDocType = SwDmDocumentType.swDmDocumentAssembly; } else if (SDocFileName.ToLower().EndsWith("slddrw")) { _nDocType = SwDmDocumentType.swDmDocumentDrawing; } else { // Not a SolidWorks file _nDocType = SwDmDocumentType.swDmDocumentUnknown; } return _nDocType; }
private int GetCurrentComponentInfo( SwDMComponent9 currentComponent, string fullPath, int level, int parentId) { if (currentComponent == null || string.IsNullOrEmpty(fullPath) || level < 0 || parentId <= 0) { return(-1); } SldDMComponentItem item = SldDMComponentItem.GenerateTheNext(); Task task = Task.Factory.StartNew(() => { item.ParentId = parentId; item.Level = level; item.FullPath = fullPath; item.FeatureName = currentComponent.Name2; item.FeatureId = currentComponent.GetID(); item.IsSuppressed = currentComponent.IsSuppressed(); item.FileName2D = EngineeringDrawingFile.Get2DFileNameIfExisting(fullPath); item.Visible = !currentComponent.IsHidden(); try { SwDmDocumentOpenError openResult; SwDMDocument17 doc = currentComponent.GetDocument2(true, this.GlobalSearchOption, out openResult) as SwDMDocument17; if (doc == null || openResult != SwDmDocumentOpenError.swDmDocumentOpenErrorNone) { Debug.WriteLine(string.Format("GetCurrentComponentInfo:couldn't open {0},the return value is :{1}", fullPath, openResult.ToString())); return; } SwDMConfiguration14 config = doc.ConfigurationManager.GetConfigurationByName(currentComponent.ConfigurationName) as SwDMConfiguration14; if (File.Exists(fullPath)) { item.AllCustomProperties = GetDocumentPropertiesViaDM.RetrievePropertiesUnderConfig( fullPath, config, GetDocumentPropertiesViaDM.PropertyNamesOftenUsed, true); } item.ConfigurationCount = doc.ConfigurationManager.GetConfigurationCount(); item.ChildrenCount = doc.GetComponentCount(); this.ResultsAtPopulating.Enqueue(item); doc.CloseDoc(); Marshal.ReleaseComObject(doc); } catch (Exception ex) { Debug.WriteLine(string.Format("GetCurrentComponentInfo:when dealing {0}, exception occurred:{1}", fullPath, ex.Message)); return; } this.CallbackGettingExtraInfo?.Invoke(ref item); Debug.WriteLine(string.Format("GetCurrentComponentInfo:got [{0}] (ID={1})", fullPath, item.Id)); }); string originalExt; SwDmDocumentType docType = SldFileExtentionChecker.CheckDM(fullPath, out originalExt); if (docType == SwDmDocumentType.swDmDocumentAssembly) { this.AllWalkingTasks.Enqueue(new ComponentAndTask_Assembly() { ParentId = parentId, TheTask = task, FullPath = fullPath, Id = item.Id }); } else { this.AllWalkingTasks.Enqueue(new ComponentAndTask_Part() { ParentId = parentId, TheTask = task, FullPath = fullPath, Id = item.Id }); } return(item.Id); }
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); }