Skip to content

How to run Javascript routines in AutoCAD .NET Plugin using ClearScript library

License

Notifications You must be signed in to change notification settings

MadhukarMoogala/AcadPlugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Run Javascript Routines in AutoCAD .NET Plugin Using ClearScript Library

Static Badge Static_Badge Static Badge

The project demonstrates a well-structured approach to running JavaScript routines in an AutoCAD .NET plugin using the ClearScript | Add scripting to your .NET applications quickly and easily.

  • Exposing AutoCAD .NET Classes: Enables access to AutoCAD functionality from JavaScript scripts.
  • Executing JavaScript Functions: Allows you to run JavaScript functions within your AutoCAD application.
  • Calling C# Functions from JavaScript: Facilitates interaction between your JavaScript code and C# functions.
public class PrintMessage
{
    private static Editor? _ed
    {
        get
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            if (doc != null)
            {
                return doc.Editor;
            }
            return null;
        }
    }
    public static void Print(string message)
    {
        _ed?.WriteMessage($"{message}\n");
    }
}

public class Commands
{
    private const string _script = @"
                        function square(x) {
                            return x*x;
                        }";
    
    //Execute JS functions in DotNet runtime


    [CommandMethod("JSRoutine")]
    public static void JSRoutine()
    {
        Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
        if (doc is null) return;
        Editor ed = doc.Editor;

        // Create aV8ScriptEngine instance, 
        using (var engine = new V8ScriptEngine())
        {
            engine.AccessContext = typeof(Commands);

             //Now expose the PrintMessage class from .NET to the script engine,
            // and then execute a JavaScript statement that writes a message to the AutoCAD Editor.
            engine.AddHostType("PrintMessage", typeof(PrintMessage));              
            engine.Execute("PrintMessage.Print('Hello from JavaScript!');");

            //Call the JS function square and retrieve the result.
            engine.Execute(_script);
            dynamic? result = engine.Script.square(5);
            ed.WriteMessage($"{Convert.ToString(result)}\n");
            

            //Execute the JS code and print the result to the AutoCAD Editor                
            engine.Execute("function print(x) {PrintMessage.Print(x); }");
            engine.Script.print(DateTime.Now.DayOfWeek.ToString());

            //Calling C# Functions from JavaScript:              
            engine.AddHostObject("Greet", new Func<string, string>((name) => $"Hello, {name}!"));              
            engine.Execute("var message = Greet('World'); PrintMessage.Print(message);");

            // examine a script object            
            engine.Execute("var person = { name: 'Fred', age: 5 }");
            ed.WriteMessage($"From Script: \n{Convert.ToString(engine.Script.person.name)}");
        }
    }
}

Requirements

Build

git clone https://github.com/MadhukarMoogala/AcadPlugin.git
cd AcadPlugin
dotnet build AcadPlugin.csproj -a x64 -c Debug

Usage

  • NETLOAD the AcadPlugin.dll

  • Execute JSROUTINE command in AutoCAD Command Line.

    result

License

MIT License

Authors

Madhukar Moogala (@galakar)

About

How to run Javascript routines in AutoCAD .NET Plugin using ClearScript library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages