Code Monkey home page Code Monkey logo

xcad's Introduction

xCAD.NET: SOLIDWORKS API development made easy

Logo

NuGet version (xCAD.NET) Build status

Templates User Guide Examples Videos

xCAD.NET is a framework for building CAD agnostic applications. It allows developers to implement complex functionality with a very simple innovative approach. This brings the best user experience to the consumers of the software.

Templates

Visual Studio and Visual Studio Code templates can be installed from NuGet

> dotnet new install Xarial.XCad.Templates

SOLIDWORKS Add-in Applications

It has never been easier to create SOLIDWORKS add-ins with toolbar and menu commands.

[ComVisible(true)]
public class XCadAddIn : SwAddInEx
{
    public enum Commands_e
    {
        Command1,
        Command2
    }

    public override void OnConnect()
    {
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += OnCommandsButtonClick;
    }

    private void OnCommandsButtonClick(Commands_e cmd)
    {
        //TODO: handle the button click
    }
}

Property Manager Pages

Framework reinvents the way you work with Property Manager Pages. No need to code a complex code behind for adding the controls and handling the values. Simply define your data model and the framework will build the suitable Property Manager Page automatically and two-way bind controls to the data model.

[ComVisible(true)]
public class IntroPmpPageAddIn : SwAddInEx
{
    [ComVisible(true)]
    public class MyPMPageData : SwPropertyManagerPageHandler
    {
        public string Text { get; set; }
        public int Number { get; set; }
        public IXComponent Component { get; set; }
    }

    private enum Commands_e
    {
        ShowPmpPage
    }

    private IXPropertyPage<MyPMPageData> m_Page;
    private MyPMPageData m_Data = new MyPMPageData();

    public override void OnConnect()
    {
        m_Page = this.CreatePage<MyPMPageData>();
        m_Page.Closed += OnPageClosed;
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += ShowPmpPage;
    }

    private void ShowPmpPage(Commands_e cmd)
    {
        m_Page.Show(m_Data);
    }

    private void OnPageClosed(PageCloseReasons_e reason)
    {
        Debug.Print($"Text: {m_Data.Text}");
        Debug.Print($"Number: {m_Data.Number}");
        Debug.Print($"Selection component name: {m_Data.Component.Name}");
    }
}

Macro Features

Complex macro features became an ease with xCAD.NET

[ComVisible(true)]
public class IntroMacroFeatureAddIn : SwAddInEx 
{
    [ComVisible(true)]
    public class BoxData : SwPropertyManagerPageHandler
    {
        public double Width { get; set; }
        public double Length { get; set; }
        public double Height { get; set; }
    }

    [ComVisible(true)]
    public class BoxMacroFeature : SwMacroFeatureDefinition<BoxData, BoxData>
    {
        public override ISwBody[] CreateGeometry(ISwApplication app, ISwDocument model, ISwMacroFeature<BoxData> feat)
        {
            var data = feat.Parameters;

            var body = (ISwBody)app.MemoryGeometryBuilder.CreateSolidBox(new Point(0, 0, 0),
                new Vector(1, 0, 0), new Vector(0, 1, 0),
                data.Width, data.Length, data.Height).Bodies.First();

            return new ISwBody[] { body };
        }

    }

    public enum Commands_e
    {
        InsertMacroFeature,
    }

    public override void OnConnect()
    {
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += OnCommandsButtonClick;
    }

    private void OnCommandsButtonClick(Commands_e cmd)
    {
        switch (cmd) 
        {
            case Commands_e.InsertMacroFeature:
                Application.Documents.Active.Features.CreateCustomFeature<BoxMacroFeature, BoxData, BoxData>();
                break;
        }
    }
}

SOLIDWORKS And Document Manager API

xCAD.NET allows to write the same code targeting different CAD implementation in a completely agnostic way. Example below demonstrates how to perform opening of assembly, traversing components recursively and closing the assembly via SOLIDWORKS API and SOLIDWORKS Document Manager API using the same code base.

static void Main(string[] args)
{
    var assmFilePath = @"C:\sample-assembly.sldasm";

    //print assembly components using SOLIDWORKS API
    var swApp = SwApplicationFactory.Create(SwVersion_e.Sw2022, ApplicationState_e.Silent);
    PrintAssemblyComponents(swApp, assmFilePath);

    //print assembly components using SOLIDWORKS Document Manager API
    var swDmApp = SwDmApplicationFactory.Create("[Document Manager Lincese Key]");
    PrintAssemblyComponents(swDmApp, assmFilePath);
}

//CAD-agnostic function to open assembly, print all components and close assembly
private static void PrintAssemblyComponents(IXApplication app, string filePath) 
{
    using (var assm = app.Documents.Open(filePath, DocumentState_e.ReadOnly))
    {
        IterateComponentsRecursively(((IXAssembly)assm).Configurations.Active.Components, 0);
    }
}

private static void IterateComponentsRecursively(IXComponentRepository compsRepo, int level) 
{
    foreach (var comp in compsRepo)
    {
        Console.WriteLine(Enumerable.Repeat("  ", level) + comp.Name);

        IterateComponentsRecursively(comp.Children, level + 1);
    }
}

Target Frameworks

xCAD.NET is compatible with multiple target frameworks: .NET Framework 4.6.1, .NET Core 3.1, .NET 6.0, .NET 7.0 and number of additional computed target frameworks (e.g. .NET Framework 4.8)

When building the SOLIDWORKS add-ins see the information below

.NET Framework

  • Run Visual Studio as an Administrator
  • Install Xarial.XCad.SolidWorks package from the nuget and create add-in class as shown above
  • Build the solution. Add-in will be automatically registered. Clean the solution to unregister the add-in.
  • Set the Embed Interop option to True for all SOLIDWORKS type libraries (e.g. SolidWorks.Interop.SldWorks.tlb, SolidWorks.Interop.SwConst.tlb, SolidWorks.Interop.SwPublished.tlb). Note this might not be required as nuget will set this flag automatically.

.NET Core/.NET 6/.NET 7

  • Run Visual Studio as an Administrator
  • Install Xarial.XCad.SolidWorks package from the nuget and create add-in class as shown above
  • Add the following property into the project file (*.csproj or *.vbproj)
<PropertyGroup>
    <EnableComHosting>true</EnableComHosting>
</PropertyGroup>
  • Build the solution. Add-in will be automatically registered. Clean the solution to unregister the add-in.

.NET Core Only

Automatic registration does not work in .NET Core and it needs to be called manually by adding the following code into the add-in (this is not required for .NET6)

[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
    SwAddInEx.RegisterFunction(t);
}

[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
    SwAddInEx.UnregisterFunction(t);
}

Watch the video demonstrations YouTube playlist of xCAD in action.

Visit User Guide page and start exploring the framework.

Unit Tests

Solution contains unit and integration tests

To execute integration tests

  • Download the Test Data
  • Unzip into the folder
  • Create an environment variable XCAD_TEST_DATA and set its value to the path of the folder above
  • To test SOLIDWORKS Document Manager, add an environment variable SW_DM_KEY and set its value to your Document Manager Key
  • Run tests

xcad's People

Contributors

artem1t avatar eddyalleman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xcad's Issues

Create API reference documentation

Need API reference (generated from XML comments) added to the xcad.xarial.com
Also need to refer all the links used in the user guide to the API reference

Build error when cleaning the solution

Build error will be shown when cleaning the solution and regasm or regsvr is run with /u switch but assembly cannot be unregistered (for example missing references). Make this error non blocking

IEnumerable<SwFeature> with ForEach

Add supporte for

Features.ForEach(feat =>{ 
      //Do something with feat like getting it's type.
});

today I have to use like that many times.

var Weldment = mPart.Features.First(feat => feat.Name.Contains("Comprimento")) as SwFeature;

BitmapButton bool not firing propertyManagerPage DataChanged Event

        [Description("Barra Quadrada")]
        [BitmapButton(typeof(Resources), nameof(Resources.Barra_Quadrada), 48, 48)]
        [ControlOptions(left: 37, top: 0)]
        public bool SquareBar { get; set; } = false; 
        private void M_Page_DataChanged()
        {
            var test = m_Data;
            
                //TODO: handle the data changing, e.g. update preview
        }

if I set a breakpoint on var test = ... and click on the button the breakpoint is never reached
On combobox change the breakpoint is reached

Set Dimension Exceptions

I Think mPart.Dimensions["A@Extrude1"]?.SetValue(Dimentions.Asize) should not break the code when there is no "A@Extrude1" in the model, or if it is really necessary we could have a mPart.Dimensions["A@Extrude1"]?.TrySetValue(Dimentions.Asize) that fails silently. Or return an bool informing if it worked or not.

Instruction on installing the addon

While developing it works great but I think we need a tutorial on instaling and common issues
I'm developping in .net core 3.1 and couldn't install on another computer.

Document events are not attached for pre-created templates

Documents commited via ISwDocumentCollection::PreCreate with a specific document type do not attach event upon commit and thus not correctly released when closed.

This applies both for opening and creating new documents.

Documents created as IXUnknownDocument work correctly

Trying to run in .net Core 3.1 cannot find System.Drawing.Common

XCad.AddIn.RK Automação: Exception: Could not load file or assembly 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
at Xarial.XCad.SolidWorks.UI.Commands.SwCommandManager.CreateIcons(CommandGroup cmdGroup, CommandGroupSpec cmdBar, IconsConverter iconsConv)
at Xarial.XCad.SolidWorks.UI.Commands.SwCommandManager.AddCommandGroupOrContextMenu(CommandGroupSpec cmdBar, Boolean isContextMenu, Nullable`1 contextMenuSelectType)
at Xarial.XCad.SolidWorks.UI.Commands.SwCommandManager.AddCommandGroup(CommandGroupSpec cmdBar)
at Xarial.XCad.UI.Commands.IXCommandManagerExtension.AddCommandGroup[TCmdEnum](IXCommandManager cmdMgr)
at RK.SWAddin.SwAddIn.OnConnect() in C:\Users\emers\Source\Repos\RK.SWAddin\RK.SWAddin\SWAddin.cs:line 41
at Xarial.XCad.SolidWorks.SwAddInEx.ConnectToSW(Object ThisSW, Int32 cookie)

Add IXComponent::Path

This should also resolve to correct path for the lightweight components or components opened in view-only mode

Exception when using indexed pixel format for icons

The following exception is thrown System.Exception: 'A Graphics object cannot be created from an image that has an indexed pixel format.' if using indexed pixel format image (256 color BMP) as an icon

256bitmap.zip

[ComVisible(true)]
    public class BitmapButtonsAddIn : Xarial.XCad.SolidWorks.SwAddInEx
    {
        [ComVisible(true)]
        public class PMPModel : SwPropertyManagerPageHandler 
        {
            [BitmapButton(typeof(Resources), nameof(Resources._256bitmap), 64, 64)]
            public Action Button1 { get; set; } = () => { };
        }

        private SwPropertyManagerPage<PMPModel> m_Page;

        public override void OnConnect()
        {
            m_Page = CreatePage<PMPModel>();
        }
    }

xcad does not compile (.NET5.0) due to RegAsm.exe

dotnet.exe build:

RegAsm : error RA0000: Could not load file or assembly 'Xarial.XCad.SolidWorks, Version=0.6.8.0, 
Culture=neutral, PublicKeyToken=60dcaf351d4060db' or one of its dependencies. The system cannot find the file specified. [C:\Users\lukasz.dlucik\Desktop\WSPOLNY\solid50\solid50.csproj]
C:\Users\lukasz.dlucik\.nuget\packages\xarial.xcad.solidworks\0.6.8\build\Xarial.XCad.SolidWorks.targets(8,4): error MSB3073: The command "%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe "C:\Users\lukasz.dlucik\Desktop\WSPOLNY\solid50\bin\Debug\net5.0\solid50.dll" /codebase" exited with code 100. [C:\Users\lukasz.dlucik\Desktop\WSPOLNY\solid50\solid50.csproj]

my .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableComHosting>true</EnableComHosting>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xarial.xcad.solidworks" Version="0.6.8" />
  </ItemGroup>

</Project>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.