018集——c# 实现CAD添加侧栏菜单(WPF控件)(CAD—C#二次开发入门)

本例实现的效果如下:CAD窗口出现内置侧栏菜单。

b059cf905b14472f830a844cf82ede6f.png

第一步:添加引用

92682b3bd20442718c03b58da05337a7.png

using UserControl = System.Windows.Controls.UserControl;
using System.Windows.Forms.Integration;//PaletteSet integration 

第二步

1d9be8e9ae4b49f4a2b10aeb5e235877.png

 b75410a5b5d94478a8d21da28f84949e.png 下面这段话最好不用全部复制,否则容易出错

e17c19e10ed54e94a417b7dd2170f34b.png

<UserControl x:Class="AcTools.UserControl1"
             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:AcTools"
             mc:Ignorable="d" 
             MinWidth="100" MinHeight="600">
    <Grid>
        <ItemsControl ItemsSource="{Binding Dic}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=Key}" Tag="{Binding Path=Value}"
                            Click="Button_Click"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

 

 3282b245cebd43c7956903544c1105c7.png

   private void Button_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
             Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b"+ bt.Tag+"\n",true,false,true);
           // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);
           
        }

 

 代码:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Runtime.CompilerServices;
using Wform = System.Windows.Forms;
using System.IO;
using System.Windows.Forms;
using Excel=  Microsoft.Office.Interop.Excel.Application;
using Autodesk.AutoCAD.Windows;
using UserControl = System.Windows.Controls.UserControl;
using System.Windows.Forms.Integration;
using Autodesk.AutoCAD.Internal;//PaletteSet integration

namespace AcTools
{
    public static class Class1
    {
        private static PaletteSet pset;

        [CommandMethod("xx")]
        public static void CaiYang()
        {   var db = Z.db;
            //var ed = Z.ed;
        
            if (pset is null)
            {
                pset = new PaletteSet("山水  QQ:443440204");
                var args = new Z.PsetArgs();
                args.Dic.Add("直线","line");
                args.Dic.Add("圆", "xx");
                args.Dic.Add("删除", "erase");
               
                var wpf = new UserControl1();
                wpf.DataContext = args;
                var host = new ElementHost()
                {
                    AutoSize = true,
                    Dock = DockStyle.Fill,
                    Child = wpf,
                };
                pset.Add("abc",host);
                pset.Visible = true;
                pset.Dock = DockSides.Left; 
                return;
            }
            pset.Visible = !pset.Visible;
        }
       
       




    }
}

 

 finally

 public class Class1
 {
     private static PaletteSet pset;

     [CommandMethod("xx")]
     public static void CaiYang()
     {
         var db = Z.db;
         //var ed = Z.ed;

         if (pset is null)
         {
             pset = new PaletteSet("山水  QQ:443440204");
             //pset.Add("AAA",new Button() {Text = "按钮" });
             //pset.Visible = true;    
             var args = new Z.PsetArgs();
             args.Dic.Add("直线", "line");
             args.Dic.Add("圆", "c");
             args.Dic.Add("删除", "erase");

             var wpf = new UserControl1();
             wpf.DataContext = args;
             var host = new ElementHost()
             {
                 AutoSize = true,
                 Dock = DockStyle.Fill,
                 Child = wpf,
             };
             pset.Add("abc", host);
             //
             var args1 = new Z.PsetArgs();
             args1.Dic.Add("sxcx", "sxcx");
             args1.Dic.Add("圆1", "c");
             args1.Dic.Add("删除1", "erase");

             var wpf1 = new UserControl1();
             wpf1.DataContext = args1;
             var host1 = new ElementHost()
             {
                 AutoSize = true,
                 Dock = DockStyle.Fill,
                 Child = wpf1,
             };
             pset.Add("abc1", host1);
             //
             pset.Visible = true;
             pset.Dock = DockSides.Left;
             return;
         }
         pset.Visible = !pset.Visible;
     }




     [CommandMethod("xx1")]
     public void Demo()
     {

     }
    
     //public Wform.Timer timer;//定时器需建在类下面
     //public static DateTime startTime;

 }

多个功能区效果:

f8510e20f6ec4fa0ad166f8e69e5013a.png

ctro+0打开隐藏菜单:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Runtime.CompilerServices;
using Wform = System.Windows.Forms;
using System.IO;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel.Application;
using Autodesk.AutoCAD.Windows;
using UserControl = System.Windows.Controls.UserControl;
//PaletteSet integration
using System.Windows.Forms.Integration;
using Autodesk.AutoCAD.Internal;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Drawing;
using Region = Autodesk.AutoCAD.DatabaseServices.Region;
using System.Windows.Shapes;
using Polyline = Autodesk.AutoCAD.DatabaseServices.Polyline;
using Line = Autodesk.AutoCAD.DatabaseServices.Line;
using Color = Autodesk.AutoCAD.Colors.Color;
using System.Threading;
using System.Timers;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using System.Windows.Input;
using Autodesk.AutoCAD.Windows.Data;
using System.Deployment.Internal;
using System.Configuration;
using Microsoft.Office.Interop.Excel;


namespace AcTools
{
    public class Class1
    {
        private static PaletteSet pset;

        [CommandMethod(nameof(SideMenu))]
        public static void SideMenu()
        {
            var db = Z.db;
            if (pset == null)
            {
                InitPaletteSet();
                return;
            }
            pset.Visible = !pset.Visible;
        }

        private static void InitPaletteSet()
        {
            pset = new PaletteSet("山水  QQ:443440204");
            //pset.Add("AAA",new Button() {Text = "按钮" });
            //pset.Visible = true;    
            var args = new Z.PsetArgs();
            args.Dic.Add("直线", "line");
            args.Dic.Add("圆", "c");
            args.Dic.Add("删除", "erase");
            var wpf = new UserControl1();//不能忘记1
            wpf.DataContext = args;
            var host = new ElementHost()
            {
                AutoSize = true,
                Dock = DockStyle.Fill,
                Child = wpf,
            };
            pset.Add("aa", host);
            pset.Visible = true;
            //必须先打开可见性再设置
            pset.Size = new(200, 600);
            pset.Dock = DockSides.Left;
            Autodesk.AutoCAD.ApplicationServices.Application.PreTranslateMessage -= AcapOnPreTranslateMessage;
            Autodesk.AutoCAD.ApplicationServices.Application.PreTranslateMessage += AcapOnPreTranslateMessage;
        }
       

        private static void AcapOnPreTranslateMessage(object sender, PreTranslateMessageEventArgs e)
        {//按下是256,257,258是按住,弹起
            string cmd;
            if (e.Message.message != 256 ||
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                is not { } doc)
                return;
            var keys = (Keys)e.Message.wParam;
            var modifierKeys = Control.ModifierKeys;
            if ((keys & Keys.NumPad0) == Keys.NumPad0 &&//ctrl+0可以弹出隐藏测蓝菜单
                (modifierKeys & Keys.Control) != 0 &&
                  (modifierKeys & Keys.Alt) == 0 &&
                  (modifierKeys & Keys.Shift) == 0)
            {
                e.Handled = true;
                cmd = "_" + nameof(SideMenu) + "\n";//
                                                    // cmd = "_" + nameof(CaiYang) + "\n";//
                cmd = cmd + "\u001b";//省略了env.cmdactive方法
                Z.doc.SendStringToExecute(cmd, true, false, false);
            }
           
        }

       
    }
}

wpf该背景颜色

d112d98c977d4e8c9e903945b9fa4e07.png3983a0ea246541c49504aece9499c2c8.png

 ifox版本:

一、usercontrols1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IfoxDemo
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);
            "ok".Print();
        }
 private void 修改块中心_Click(object sender, RoutedEventArgs e)
        {
            //var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
           // Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);
            "ok".Print();
        }
        private void 修改块名_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 光反射_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 全部包围盒_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 全部属性_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 画向量_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 旋转光线_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 选择块名创建块_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
        private void 连续标数_Click(object sender, RoutedEventArgs e)
        {
            var bt = (System.Windows.Controls.Button)sender;
            //Z.ed.WriteMessage("\u001b"+"line\n");
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + bt.Tag + "\n", true, false, true);
            // Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\u001b" + "xx" + "\n", true, false, true);

        }
    }
}

二、主程序

    private static PaletteSet pset;
    [CommandMethod("xx")]
    public static void WPFH()
    {
        var db = Env.Database;
        //var ed = Z.ed;

        if (pset is null)
        {
            pset = new PaletteSet("山水  QQ:443440204");
            //pset.Add("AAA",new Button() {Text = "按钮" });
            //pset.Visible = true;    
            //var args = new PsetArgs();
            //args.Dic.Add("直线", "line");
            //args.Dic.Add("圆", "c");
            //args.Dic.Add("删除", "erase");

            var wpf = new UserControl1();
            //wpf.DataContext = args;
            var host = new ElementHost()
            {
                AutoSize = true,
                Dock = DockStyle.Fill,
                Child = wpf,
            };
            pset.Add("abc", host);
            //
            var args1 = new PsetArgs();
            args1.Dic.Add("sxcx", "sxcx");
            args1.Dic.Add("圆1", "c");
            args1.Dic.Add("删除1", "erase");

            var wpf1 = new UserControl1();
            wpf1.DataContext = args1;
            var host1 = new ElementHost()
            {
                AutoSize = true,
                Dock = DockStyle.Fill,
                Child = wpf1,
            };
            pset.Add("abc1", host1);
            //
            pset.Visible = true;
            pset.Dock = DockSides.Left;
            return;
        }
       // pset.Visible = !pset.Visible;

    }
    public class PsetArgs
    {
        public PsetArgs() { }
        public Dictionary<string, string> Dic { get; set; } = new Dictionary<string, string>();
    }

保留上次位置d4541ccff599405fba60bcb14deec4c8.png

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值