.NET6: Developing modern three-dimensional industrial software based on WPF

Posted by Welling on Sat, 26 Feb 2022 18:23:31 +0100

Python WeChat Subscription Applet Course Video

https://edu.csdn.net/course/detail/36074

Python Actual Quantitative Transaction Finance System

https://edu.csdn.net/course/detail/35475
As a modern industrial software, the ability to provide programmable scripting is essential. Scripts can be used for secondary development and automated testing of programs. This article combines the ability of AnyCAD to support Python scripting and WPF to quickly develop CAD software with script editor.

1 Add script plugins

Create a WPF control project named Rapid.ScriptPy.Plugin. Refer to " Developing modern three-dimensional industrial software based on WPF (6) Add a ScriptRibbonTab and an additional Run Script button.

<Fluent:RibbonTabItem xmlns:Fluent="urn:fluent-ribbon" x:Class="Rapid.ScriptPy.Plugin.UI.ScriptRibbonTab"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 xmlns:local="clr-namespace:Rapid.ScriptPy.Plugin.UI"
 mc:Ignorable="d" 
 Header="Script"
 d:DesignHeight="100" d:DesignWidth="400">
    <Fluent:RibbonGroupBox Header="Basic Body" IsLauncherVisible="False" Margin="7,0,0,0">
        <Fluent:Button Header="Function" Size="Large" Command="{x:Static local:ScriptRibbonTab.ExecuteCommand}"
 CommandParameter="runPy" Margin="0,0,7,0"/>
    Fluent:RibbonGroupBox>
Fluent:RibbonTabItem>

namespace Rapid.ScriptPy.Plugin.UI
{
    /// 
    /// ScriptRibbonTab.xaml's interaction logic
    /// 
    public partial class ScriptRibbonTab
    {
        public static readonly RoutedCommand ExecuteCommand = new("Rapid.ScriptPy", typeof(ScriptRibbonTab));

        public ScriptRibbonTab()
        {
            InitializeComponent();

            CommandBindings.Add(new CommandBinding(ExecuteCommand, OnExecuteCommand));
        }

        private void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
        {

        }
    }
}

2 Script Editor

There are many code editors based on WPF with syntax highlighting, which are used in this article AvalonEdit As a Python code editing control.

2.1 Add Assembly

At nuget.org searches AvalonEdit for Rapid.CAX and Rapid.ScriptPy.Plugin adds assembly references:

2.2 Add Editor Control

In MainWindow. Add a script editor after the 3-D control in XAML displays the window:

<ad:LayoutPanel Orientation="Vertical">
    <ad:LayoutDocumentPane IsMaximized="True">
        <ad:LayoutDocument Title="Python Script" CanClose="False">
            
            <avalonEdit:TextEditor
 xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
 Name="textEditor"
 SyntaxHighlighting="Python"
 FontFamily="Consolas"
 FontSize="12pt" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>
        ad:LayoutDocument>
    ad:LayoutDocumentPane>
ad:LayoutPanel>

Compile and run:

As you can see, our program has the ability to highlight Python syntax with ease.
Of course, AvalonEdit also has advanced features such as hints, code block folding, and so on.

3 Integrated script

Our goal is at Rapid. ScriptPy. To run a script in Plugin, you first need to expose a way to get TextEditor for MainWindow:

   public partial class MainWindow
    {
        ...
        public ICSharpCode.AvalonEdit.TextEditor GetTextEditor()
        {
            return this.textEditor;
        }
        ...
    }

Respond to run command:

    public partial class ScriptRibbonTab
    {
       //...
       private void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
        {
            switch (e.Parameter.ToString())
            {
                case "runPy":
                    {
                        var textEditor = MainWindow.Instance()?.GetTextEditor();
                        if(textEditor?.Text?.Length > 0)
                        {
                            AnyCAD.Py.PyScript.Instance().Run(textEditor?.Text);
                        }
                    }
                    break;
            }
        }
    }

Use AnyCAD.Py.PyScript.Instance().Run(content) can run script content directly. AnyCAD.Py.PyScript.Instance().RunFile(filePath) can run scripts within a file.

Compile and run to test:

box = AnyCAD.ShapeBuilder.MakeBox(AnyCAD.GAx2(), 1,1,1)
theViewer.ShowTopoShape(box, None)
theViewer.RequestUpdate(AnyCAD.EnumUpdateFlags_Scene)

4 Summary

Python is at the top of recent programming language charts. Adding Python scripting support to your program can greatly reduce the threshold for secondary development. Based on the open Python API of AnyCAD, you can easily use Python scripts to create and analyze models.

AvalonEdit is a great code editing control, and we can further improve advanced features such as smart tips, code block folding, and provide a user experience.

Code for this article: https://gitee.com/anycad/RapidCAX/tree/new-ui/

5 Related Links

[1] AvalonEdit: http://avalonedit.net/documentation/

[2] .NET6: Developing modern three-dimensional industrial software based on WPF (6)

[3] .NET6: Developing modern three-dimensional industrial software based on WPF (5)

[4] .NET6: Developing modern three-dimensional industrial software based on WPF (4)

[5] .NET6: Developing modern three-dimensional industrial software based on WPF (3)

[6] .NET6: Developing modern three-dimensional industrial software based on WPF (2)

[7] .NET6: Developing modern three-dimensional industrial software based on WPF (1)
[8] This article code gitee/anycad/RapidCAX/

Topics: Python .NET WPF computer microsoft