/// <summary> /// Called when the active model has changed /// </summary> /// <returns></returns> private int ActiveModelChanged() { // Wrap any error SolidDnaErrors.Wrap(() => { // If we are currently loading a file... if (mFileLoading != null) { // Check the active document using (var activeDoc = new Model((ModelDoc2)mBaseObject.ActiveDoc)) { // If this is the same file that is currently being loaded, ignore this event if (activeDoc != null && string.Equals(mFileLoading, activeDoc.FilePath, StringComparison.OrdinalIgnoreCase)) { return; } } } // If we got here, it isn't the current document so reload the data ReloadActiveModelInformation(); }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationActiveModelChangedError, Localization.GetString("SolidWorksApplicationActiveModelChangedError")); // NOTE: 0 is OK, anything else is an error return(0); }
/// <summary> /// Called after a file has finished opening /// </summary> /// <param name="filename">The filename to the file being opened</param> /// <returns></returns> private int FileOpenPostNotify(string filename) { // Wrap any error SolidDnaErrors.Wrap(() => { // If this is the file we were opening... if (string.Equals(filename, mFileLoading, StringComparison.OrdinalIgnoreCase)) { // File has been loaded // So clear loading flag mFileLoading = null; // And update all properties and models ReloadActiveModelInformation(); // Inform listeners FileOpened(filename, mActiveModel); } }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationFilePostOpenError, Localization.GetString("SolidWorksApplicationFilePostOpenError")); // NOTE: 0 is OK, anything else is an error return(0); }
/// <summary> /// Sets the material for the model /// </summary> /// <param name="material">The material</param> /// <param name="configuration">The configuration to set the material on, null for the default</param> /// public void SetMaterial(Material material, string configuration = null) { // Wrap any error SolidDnaErrors.Wrap(() => { // Make sure we are a part if (!IsPart) { throw new InvalidOperationException(Localization.GetString("SolidWorksModelSetMaterialModelNotPartError")); } // If the material is null, remove the material if (material == null || !material.DatabaseFileFound) { AsPart().SetMaterialPropertyName2(string.Empty, string.Empty, string.Empty); } // Otherwise set the material else { AsPart().SetMaterialPropertyName2(configuration, material.Database, material.Name); } }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelSetMaterialError, Localization.GetString("SolidWorksModelSetMaterialError")); }
/// <summary> /// Gets the current SolidWorks version information /// </summary> /// <returns></returns> protected SolidWorksVersion GetSolidWorksVersion() { // Wrap any error return(SolidDnaErrors.Wrap(() => { // Get version string (such as 23.2.0 for 2015 SP2.0) var revisionNumber = mBaseObject.RevisionNumber(); // Get revision string (such as sw2015_SP20) string revisionString; // Get build number (such as d150130.002) string buildNumber; // Get the hotfix string string hotfixString; mBaseObject.GetBuildNumbers2(out revisionString, out buildNumber, out hotfixString); return new SolidWorksVersion { RevisionNumber = revisionNumber, Revision = revisionString, BuildNumber = buildNumber, Hotfix = hotfixString }; }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationVersionError, Localization.GetString("SolidWorksApplicationVersionError"))); }
/// <summary> /// Create a command group from a list of <see cref="CommandManagerItem"/> items /// </summary> /// <param name="title">Name of the CommandGroup to create (see Remarks)</param> /// <param name="items">The command items to add</param> /// <param name="iconListsPath">The icon list absolute path based on a string format of the absolute path to the icon list images, replacing {0} with the size. /// For example C:\Folder\myiconlist{0}.png</param> /// <param name="tooltip">Tool tip for the CommandGroup</param> /// <param name="hint">Text displayed in SOLIDWORKS status bar when a user's mouse pointer is over the CommandGroup</param> /// <param name="position">Position of the CommandGroup in the CommandManager for all document templates. /// NOTE: Specify 0 to add the CommandGroup to the beginning of the CommandMananger, or specify -1 to add it to the end of the CommandManager. /// NOTE: You can also use ICommandGroup::MenuPosition to control the position of the CommandGroup in specific document templates.</param> /// <param name="ignorePreviousVersion">True to remove all previously saved customization and toolbar information before creating a new CommandGroup, false to not. /// Call CommandManager.GetGroupDataFromRegistry before calling this method to determine how to set IgnorePreviousVersion. Set IgnorePreviousVersion to true to prevent SOLIDWORKS from saving the current toolbar setting to the registry, even if there is no previous version.</param> /// <param name="hasMenu">Whether the CommandGroup should appear in the Tools dropdown menu.</param> /// <param name="hasToolbar">Whether the CommandGroup should appear in the Command Manager and as a separate toolbar.</param> /// <returns></returns> public CommandManagerGroup CreateCommands(string title, List <CommandManagerItem> items, string iconListsPath = "", string tooltip = "", string hint = "", int position = -1, bool ignorePreviousVersion = true, bool hasMenu = true, bool hasToolbar = true) { // Wrap any error creating the taskpane in a SolidDna exception return(SolidDnaErrors.Wrap(() => { // Lock the list lock (mCommandGroups) { // Create the command group var group = CreateCommandGroup(title, items, tooltip, hint, position, ignorePreviousVersion, hasMenu, hasToolbar); // Set icon list group.SetIconLists(iconListsPath); // Create the group group.Create(this); // Add this group to the list mCommandGroups.Add(group); // Return the group return group; } }, SolidDnaErrorTypeCode.SolidWorksCommandManager, SolidDnaErrorCode.SolidWorksCommandGroupCreateError, Localization.GetString("ErrorSolidWorksCommandGroupAddError"))); }
/// <summary> /// Read the material from the model /// </summary> /// <returns></returns> public Material GetMaterial() { // Wrap any error return(SolidDnaErrors.Wrap(() => { // Get the Id's var idString = mBaseObject.MaterialIdName; // Make sure we have some data if (idString == null || !idString.Contains("|")) { return null; } // The Id string is split by pipes | var ids = idString.Split('|'); // We need at least the first and second // (first is database file name, second is material name) if (ids.Length < 2) { throw new ArgumentOutOfRangeException(Localization.GetString("SolidWorksModelGetMaterialIdMissingError")); } // Extract data var databaseName = ids[0]; var materialName = ids[1]; // See if we have a database file with the same name var fullPath = Dna.Application.GetMaterials()?.FirstOrDefault(f => string.Equals(databaseName, Path.GetFileNameWithoutExtension(f.Database), StringComparison.InvariantCultureIgnoreCase)); var found = fullPath != null; // Now we have the file, try and find the material from it if (found) { var foundMaterial = Dna.Application.FindMaterial(fullPath.Database, materialName); if (foundMaterial != null) { return foundMaterial; } } // If we got here, the material was not found // So fill in as much information as we have return new Material { Database = databaseName, Name = materialName }; }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelGetMaterialError, Localization.GetString("SolidWorksModelGetMaterialError"))); }
/// <summary> /// Adds a control (Windows <see cref="System.Windows.Forms.UserControl"/>) to the taskpane /// that has been exposed to COM and has a given ProgId /// </summary> /// <typeparam name="T">The type of UserControl being created</typeparam> /// <param name="progId">The [ProgId()] attribute value adorned on the UserControl class</param> /// <param name="licenseKey">The license key (for specific SolidWorks add-in types)</param> /// <returns></returns> public async Task <T> AddControlAsync <T>(string progId, string licenseKey) { // Wrap any error creating the taskpane in a SolidDna exception return(SolidDnaErrors.Wrap <T>(() => { // Attempt to create the taskpane return (T)BaseObject.AddControl(progId, licenseKey); }, SolidDnaErrorTypeCode.SolidWorksTaskpane, SolidDnaErrorCode.SolidWorksTaskpaneAddControlError, await Localization.GetStringAsync("ErrorSolidWorksTaskpaneAddControlError"))); }
/// <summary> /// Saves a file to the specified path, with the specified options /// </summary> /// <param name="savePath">The path of the file to save as</param> /// <param name="version">The version</param> /// <param name="options">Any save as options</param> /// <param name="pdfExportData">The PDF Export data if the save as type is a PDF</param> /// <returns></returns> public ModelSaveResult SaveAs(string savePath, SaveAsVersion version = SaveAsVersion.CurrentVersion, SaveAsOptions options = SaveAsOptions.None, PdfExportData pdfExportData = null) { // Start with a successful result var results = new ModelSaveResult(); // Set errors and warnings to none to start with var errors = 0; var warnings = 0; // Wrap any error return(SolidDnaErrors.Wrap(() => { // Try and save the model using the SaveAs method BaseObject.Extension.SaveAs(savePath, (int)version, (int)options, pdfExportData?.ExportData, ref errors, ref warnings); // If this fails, try another way if (errors != 0) { BaseObject.SaveAs4(savePath, (int)version, (int)options, ref errors, ref warnings); } // Add any warnings results.Warnings = (SaveAsWarnings)warnings; // Add any errors results.Errors = (SaveAsErrors)errors; // If successful, and this is not a new file // (otherwise the RCW changes and SolidWorksApplication has to reload ActiveModel)... if (results.Successful && HasBeenSaved) { // Reload model data ReloadModelData(); } // If we have not been saved, SolidWorks never fires any FileSave events at all // so request a refresh of the ActiveModel. That is the best we can do // as this RCW is now invalid. If this model is not active when saved then // it will simply reload the active models information if (!HasBeenSaved) { SolidWorksEnvironment.Application.RequestActiveModelChanged(); } // Return result return results; }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelSaveAsError, Localization.GetString("SolidWorksModelGetMaterialError"))); }
/// <summary> /// Attempts to find the material from a SolidWorks material database file (sldmat) /// If found, returns the full information about the material /// </summary> /// <param name="database">The full path to the database</param> /// <param name="materialName">The material name to find</param> /// <returns></returns> public Material FindMaterial(string database, string materialName) { // Wrap any error return(SolidDnaErrors.Wrap(() => { // Get all materials from the database var materials = GetMaterials(database); // Return if found the material with the same name return materials?.FirstOrDefault(f => string.Equals(f.Name, materialName, StringComparison.InvariantCultureIgnoreCase)); }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationFindMaterialsError, Localization.GetString("SolidWorksApplicationFindMaterialsError"))); }
/// <summary> /// Called when SolidWorks is idle /// </summary> /// <returns></returns> private int OnIdleNotify() { // Wrap any error SolidDnaErrors.Wrap(() => { // Inform listeners Idle(); }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationError, Localization.GetString("SolidWorksApplicationOnIdleNotificationError")); // NOTE: 0 is OK, anything else is an error return(0); }
/// <summary> /// Casts the object to a <see cref="ModelDisplayDimension"/> /// Check with <see cref="IsDimension"/> first to assure that it is this type /// </summary> /// <param name="action">The Dimension is passed into this action to be used within it</param> public void AsDimension(Action <ModelDisplayDimension> action) { // Wrap any error SolidDnaErrors.Wrap(() => { // Create feature using (var model = new ModelDisplayDimension((IDisplayDimension)mBaseObject)) { // Run action action(model); } }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelSelectedObjectCastError, Localization.GetString("SolidWorksModelSelectedObjectCastError")); }
/// <summary> /// Gets the <see cref="ModelFeature"/> of the item in the feature tree based on its name /// </summary> /// <param name="featureName">Name of the feature</param> /// <returns>The <see cref="ModelFeature"/> for the named feature</returns> public void GetFeatureByName(string featureName, Action <ModelFeature> action) { // Wrap any error SolidDnaErrors.Wrap(() => { // Create feature using (var model = new ModelFeature((Feature)mBaseObject.FeatureByName(featureName))) { // Run action action(model); } }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelAssemblyGetFeatureByNameError, Localization.GetString(nameof(SolidDnaErrorCode.SolidWorksModelAssemblyGetFeatureByNameError))); }
/// <summary> /// Saves a file to the specified path, with the specified options /// </summary> /// <param name="savePath">The path of the file to save as</param> /// <param name="version">The version</param> /// <param name="options">Any save as options</param> /// <param name="pdfExportData">The PDF Export data if the save as type is a PDF</param> /// <returns></returns> public ModelSaveResult SaveAs(string savePath, SaveAsVersion version = SaveAsVersion.CurrentVersion, SaveAsOptions options = SaveAsOptions.None, PdfExportData pdfExportData = null) { // Start with a successful result var results = new ModelSaveResult(); // Set errors and warnings to none to start with var errors = 0; var warnings = 0; // Wrap any error return(SolidDnaErrors.Wrap(() => { // Try and save the model using the SaveAs method mBaseObject.Extension.SaveAs(savePath, (int)version, (int)options, pdfExportData?.ExportData, ref errors, ref warnings); // If this fails, try another way if (errors != 0) { mBaseObject.SaveAs4(savePath, (int)version, (int)options, ref errors, ref warnings); } // Add any warnings results.Warnings = (SaveAsWarnings)warnings; // Add any errors results.Errors = (SaveAsErrors)errors; // If successful... if (results.Successful) { // Reload model data ReloadModelData(); } // Return result return results; }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelSaveAsError, Localization.GetString("SolidWorksModelGetMaterialError"))); }
/// <summary> /// Attempts to create /// </summary> /// <param name="iconPath">An absolute path to an icon to use for the taskpane (ideally 37x37px)</param> /// <param name="toolTip">The title text to show at the top of the taskpane</param> public async Task <Taskpane> CreateTaskpaneAsync(string iconPath, string toolTip) { // Wrap any error creating the taskpane in a SolidDna exception return(SolidDnaErrors.Wrap <Taskpane>(() => { // Attempt to create the taskpane var comTaskpane = mBaseObject.CreateTaskpaneView2(iconPath, toolTip); // If we fail, return null if (comTaskpane == null) { return null; } // If we succeed, create SolidDna object return new Taskpane(comTaskpane); }, SolidDnaErrorTypeCode.SolidWorksTaskpane, SolidDnaErrorCode.SolidWorksTaskpaneCreateError, await Localization.GetStringAsync("ErrorSolidWorksTaskpaneCreateError"))); }
/// <summary> /// Called before a file has started opening /// </summary> /// <param name="filename">The filename to the file being opened</param> /// <returns></returns> private int FileOpenPreNotify(string filename) { // Don't handle the ActiveModelDocChangeNotify event for file open events // - wait until the file is open instead // NOTE: We need to check if the variable already has a value because in the case of a drawing // we get multiple pre events - one for the drawing, and one for each model in it, // we're only interested in the first // Wrap any error SolidDnaErrors.Wrap(() => { if (mFileLoading == null) { mFileLoading = filename; } }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationFilePreOpenError, Localization.GetString("SolidWorksApplicationFilePreOpenError")); // NOTE: 0 is OK, anything else is an error return(0); }
/// <summary> /// Get's a list of all materials in SolidWorks /// </summary> /// <param name="database">If specified, limits the results to the specified database full path</param> public List <Material> GetMaterials(string database = null) { // Wrap any error return(SolidDnaErrors.Wrap(() => { // Create an empty list var list = new List <Material>(); // If we are using a specified database, use that if (database != null) { ReadMaterials(database, ref list); } else { // Otherwise, get all known ones // Get the list of material databases (full paths to sldmat files) var databases = (string[])mBaseObject.GetMaterialDatabases(); // Get materials from each if (databases != null) { foreach (var d in databases) { ReadMaterials(d, ref list); } } } // Order the list return list.OrderBy(f => f.DisplayName).ToList(); }, SolidDnaErrorTypeCode.SolidWorksApplication, SolidDnaErrorCode.SolidWorksApplicationGetMaterialsError, Localization.GetString("SolidWorksApplicationGetMaterialsError"))); }
/// <summary> /// Gets the mass properties of a part/assembly /// </summary> /// <param name="doNotThrowOnError">If true, don't throw on errors, just return empty mass</param> /// <returns></returns> public MassProperties GetMassProperties(bool doNotThrowOnError = true) { // Wrap any error return(SolidDnaErrors.Wrap(() => { // Make sure we are a part if (!Parent.IsPart && !Parent.IsAssembly) { if (doNotThrowOnError) { return new MassProperties(); } else { throw new InvalidOperationException(Localization.GetString("SolidWorksModelGetMassModelNotPartError")); } } double[] massProps = null; var status = -1; // // SolidWorks 2016 is the start of support for MassProperties2 // // Tested on 2015 and crashes, so drop-back to lower version for support // if (SolidWorksEnvironment.Application.SolidWorksVersion.Version < 2016) { // NOTE: 2 is best accuracy massProps = (double[])BaseObject.GetMassProperties(2, ref status); } else { // NOTE: 2 is best accuracy massProps = (double[])BaseObject.GetMassProperties2(2, out status, false); } // Make sure it succeeded if (status == (int)swMassPropertiesStatus_e.swMassPropertiesStatus_UnknownError) { if (doNotThrowOnError) { return new MassProperties(); } else { throw new InvalidOperationException(Localization.GetString("SolidWorksModelGetMassModelStatusFailed")); } } // If we have no mass, return empty else if (status == (int)swMassPropertiesStatus_e.swMassPropertiesStatus_NoBody) { return new MassProperties(); } // Otherwise we have the properties so return them return new MassProperties(massProps); }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelGetMassPropertiesError, Localization.GetString("SolidWorksModelGetMassPropertiesError"))); }