public void Place(PlaceSpec place, Model.CircuitComponent part) { var partPath = Path.Combine(workingFolder, part.PartName + ".sldprt"); if (!File.Exists(partPath)) { File.WriteAllBytes(partPath, part.Data); } var pattDoc = sw.OpenDoc6(partPath, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref error, ref warning); // Activate main assembly sw.ActivateDoc3(doc.GetTitle(), true, (int)swRebuildOnActivation_e.swUserDecision, ref error); // In context of top projection place goes X -> -Z, Y -> X, Z -> Y var x = (place.YMm) / 1000; var y = 0.0; var z = (place.XMm) / 1000; var component = ((AssemblyDoc)doc).AddComponent5(Path.GetFileNameWithoutExtension(partPath), (int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", false, "", x, y, z); RotatePartY(component, new[] { place.YMm / 1000, 0.0, -place.XMm / 1000 }, place.Angle); }
public static void SaveAsIges(this IBody2 body, string igesFile, bool hidden = false) { SldWorks app = SwAddinBase.Active.SwApp; var doc = app.CreateHiddenDocument(hidden: hidden); doc.Extension.SetUserPreferenceInteger ((int)(swUserPreferenceIntegerValue_e.swUnitSystem) , 0 , ((int)(swUnitSystem_e.swUnitSystem_MKS))); try { var partDoc = (PartDoc)doc; partDoc.CreateFeatureFromBody3(body, false, 0); int errorsi = 0; int warningsi = 0; app.ActivateDoc3 (doc.GetTitle(), false, (int)swRebuildOnActivation_e.swRebuildActiveDoc, ref errorsi); // http://help.solidworks.com/2013/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SaveAs.html // Note the doc says: // To save as an IGES, STL, or STEP file, the document to convert must be the active document. Before calling this method: // Call ISldWorks::ActivateDoc3 to make the document to convert the active document. // Call ISldWorks::ActiveDoc to get the active document. var status = doc // Note that this does not return the correct doc if the doc is hidden .Extension .SaveAs (igesFile , (int)swSaveAsVersion_e.swSaveAsCurrentVersion , (int)swSaveAsOptions_e.swSaveAsOptions_Silent , null , ref errorsi , ref warningsi ); if (!status) { throw new Exception($"Failed to save {igesFile}. Got error bitmask {errorsi}"); } } finally { app.QuitDoc(doc.GetTitle()); } }
public static void ActivateDoc(SldWorks iswApp) { int err = -1; int warn = -1; iswApp.OpenDoc6(AppDomain.CurrentDomain.BaseDirectory + @"RectanglePlug\PlugTopBox.SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_AutoMissingConfig, "圆壳", ref err, ref warn); iswApp.OpenDoc6(AppDomain.CurrentDomain.BaseDirectory + @"RectanglePlug\PlugWire.SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_AutoMissingConfig, "", ref err, ref warn); ModelDoc2 Doc = iswApp.ActiveDoc; MessageBox.Show("当前激活文档:" + Doc.GetTitle()); Doc = iswApp.ActivateDoc3("PlugTopBox.SLDPRT", true, (int)swRebuildOnActivation_e.swRebuildActiveDoc, ref err); MessageBox.Show("文档:" + Doc.GetTitle() + "被激活"); Doc = iswApp.ActiveDoc; MessageBox.Show("当前激活文档:" + Doc.GetTitle()); }
private Bitmap GetViewImage(int width, int height, string namedView = null, int swStandardView = 0) { // Where swStandardView is one of the following: // swStandardViews_e.swIsometricView = 7 // swStandardViews_e.swTrimetricView = 8 // swStandardViews_e.swDimetricView = 9 // current view = 0 // named view = -1 ModelView swModelView = this.swMainModel.ActiveView; int errors = 0; if (swModelView == null) { swApp.ActivateDoc3(modelName, false, (int)swRebuildOnActivation_e.swRebuildActiveDoc, ref errors); } swModelView = this.swMainModel.ActiveView; swModelView.FrameState = (int)swWindowState_e.swWindowMaximized; // get current user settings bool prefViewDisplayHideAllTypes = swMainModel.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swViewDisplayHideAllTypes); // change settings for image extraction swMainModel.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swViewDisplayHideAllTypes, true); swMainModel.Extension.InsertScene("\\scenes\\01 basic scenes\\11 white kitchen.p2s"); swModelView.DisplayMode = (int)swViewDisplayMode_e.swViewDisplayMode_HiddenLinesRemoved; if (namedView != null | swStandardView != 0) { // see enumeration swStandardViews_e swMainModel.ShowNamedView2(namedView, swStandardView); } swMainModel.ViewZoomtofit2(); // extract the image string tmpFilePathName = Path.GetTempFileName(); swMainModel.SaveBMP(tmpFilePathName, width, height); Bitmap bmp = (Bitmap)FromFile(tmpFilePathName); File.Delete(tmpFilePathName); // apply original user settings swMainModel.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swViewDisplayHideAllTypes, prefViewDisplayHideAllTypes); swModelView.DisplayMode = (int)swViewDisplayMode_e.swViewDisplayMode_ShadedWithEdges; return(bmp); }
public LinkedList <PageInfo> CreateDrawings(SldWorks _swApp, LinkedList <PageInfo> _ll) { int dt = (int)swDocumentTypes_e.swDocDRAWING; int err = 0; int warn = 0; int saveVersion = (int)swSaveAsVersion_e.swSaveAsCurrentVersion; int saveOptions = (int)swSaveAsOptions_e.swSaveAsOptions_Silent; bool success; var nd_ = _ll.First; while (nd_ != null) { if (nd_.Value.fileInfo != null) { FileInfo slddrw_ = nd_.Value.fileInfo; string newName = slddrw_.Name.Replace(@".SLDDRW", @".PDF"); FileInfo tmpFile = new FileInfo(string.Format(@"{0}\{1}", Path.GetTempPath(), newName)); FileSystemEventArgs fsea_ = new FileSystemEventArgs(WatcherChangeTypes.All, Path.GetDirectoryName(tmpFile.FullName), tmpFile.Name); OnOpening(fsea_); if (slddrw_.FullName.ToUpper().EndsWith(@"SLDDRW")) { _swApp.OpenDocSilent(slddrw_.FullName, dt, ref err); _swApp.ActivateDoc3(slddrw_.FullName, true, (int)swRebuildOnActivation_e.swDontRebuildActiveDoc, ref err); DrawingDoc dd_ = (DrawingDoc)_swApp.ActiveDoc; ModelDoc2 md_ = (ModelDoc2)_swApp.ActiveDoc; if (md_ != null) { success = md_.SaveAs4(tmpFile.FullName, saveVersion, saveOptions, ref err, ref warn); } nd_.Value.fileInfo = tmpFile; OnClosing(fsea_); _swApp.CloseDoc(slddrw_.FullName); } } nd_ = nd_.Next; } return(_ll); }
/// <summary> /// Opens and saves models referenced in the open drawing. /// </summary> /// <returns>Returns a <see cref="System.Boolean"/>, should you need one.</returns> public Boolean ExportEDrawings() { ModelDoc2 currentDoc = swModel; string docName = currentDoc.GetPathName(); String sourceName = Path.GetFileNameWithoutExtension(docName); String altPath = Path.GetDirectoryName(docName).Substring(3); ModelDocExtension swModExt = swModel.Extension; String Rev = GetRev(swModExt); String fFormat = String.Empty; List <string> ml = get_list_of_open_docs(); List <String> EdrwTarget = new List <string>(); List <String> STEPTarget = new List <string>(); Boolean measurable = true; Int32 options = 0; Int32 errors = 0; switch (modelType) { case swDocumentTypes_e.swDocASSEMBLY: fFormat = ".EASM"; break; case swDocumentTypes_e.swDocPART: fFormat = ".EPRT"; break; default: ExportPDFException e = new ExportPDFException("Document type error."); //e.Data.Add("who", System.Environment.UserName); //e.Data.Add("when", DateTime.Now); throw e; } swApp.ActivateDoc3(sourcePath, true, options, ref errors); swModel = (ModelDoc2)swApp.ActiveDoc; Configuration swConfig = (Configuration)swModel.GetActiveConfiguration(); swFrame.SetStatusBarText("Positioning model."); swModel.ShowNamedView2("*Dimetric", 9); swModel.ViewZoomtofit2(); if (!swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure)) { swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure, true); measurable = false; } else { measurable = true; } EdrwTarget.Add(String.Format("{0}{1}\\{2}{3}", APathSet.KPath, altPath, sourceName, fFormat)); EdrwTarget.Add(String.Format("{0}{1}\\{2}{3}{4}", APathSet.GPath, altPath, sourceName, Rev, fFormat)); if (APathSet.ExportSTEP) { STEPTarget.Add(String.Format("{0}{1}\\{2}{3}", APathSet.KPath, altPath, sourceName, @".STEP")); STEPTarget.Add(String.Format("{0}{1}\\{2}{3}{4}", APathSet.GPath, altPath, sourceName, Rev, @".STEP")); } Boolean success = SaveSTEPorEDrw(EdrwTarget); if (STEPTarget.Count > 0) { success = SaveSTEPorEDrw(STEPTarget); } if (!measurable) { swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure, false); } if (!ml.Contains(swModel.GetPathName())) { swFrame.SetStatusBarText("Closing model."); swApp.CloseDoc(sourcePath); } else if (APathSet.SaveFirst) { swModel.SaveSilent(); } int err = 0; swApp.ActivateDoc3(currentDoc.GetTitle(), true, (int)swRebuildOnActivation_e.swDontRebuildActiveDoc, ref err); return(success); }
private string LoadActiveModel() { /// <summary>Load active model /// <list type="bullet"> /// <listheader> /// <description>Set model properties</description> /// </listheader> /// <item>Model file name with path</item> /// <item>Model file name</item> /// <item>Referenced configuration</item> /// </list> /// <returns>Error message string</returns> /// </summary> ModelDoc2 swDoc; SolidWorks.Interop.sldworks.View swDrawView; swDocumentTypes_e swDocType; string strModelFile; string strModelName; string strConfigName = null; int intErrors = 0; int intWarnings = 0; // get the active document swDoc = (ModelDoc2)this.swApp.ActiveDoc; if (swDoc == null) { return("Could not acquire an active document"); } // Check for the correct doc type strModelFile = swDoc.GetPathName(); strModelName = strModelFile.Substring(strModelFile.LastIndexOf("\\") + 1, strModelFile.Length - strModelFile.LastIndexOf("\\") - 1); swDocType = (swDocumentTypes_e)swDoc.GetType(); // get model document from drawing document if (swDocType == swDocumentTypes_e.swDocDRAWING) { swDrawDoc = (DrawingDoc)swDoc; swDrawView = (SolidWorks.Interop.sldworks.View)swDrawDoc.GetFirstView(); swDrawView = (SolidWorks.Interop.sldworks.View)swDrawView.GetNextView(); strModelFile = swDrawView.GetReferencedModelName(); strConfigName = swDrawView.ReferencedConfiguration; strModelName = strModelFile.Substring(strModelFile.LastIndexOf("\\") + 1, strModelFile.Length - strModelFile.LastIndexOf("\\") - 1); swDocType = (swDocumentTypes_e)GetTypeFromString(strModelFile); if (swDocType != swDocumentTypes_e.swDocASSEMBLY & swDocType != swDocumentTypes_e.swDocPART) { return("error getting file type from drawing view's referenced model"); } // Try to load the model file try { swMainModel = swApp.OpenDoc6(strModelFile, (int)swDocType, (int)swOpenDocOptions_e.swOpenDocOptions_LoadModel, strConfigName, ref intErrors, ref intWarnings); swMainModel = swApp.ActivateDoc3(strModelFile, true, (int)swRebuildOnActivation_e.swDontRebuildActiveDoc, intErrors); } catch (Exception e) { return(e.Message); } } else { swMainModel = swDoc; } swModelWrap = new SwModelWrapper(this, swApp, swDocType, swMainModel, strConfigName: null); return(null); }