本例实现的效果如下:CAD窗口出现内置侧栏菜单。
第一步:添加引用
using UserControl = System.Windows.Controls.UserControl;
using System.Windows.Forms.Integration;//PaletteSet integration
第二步
下面这段话最好不用全部复制,否则容易出错
<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>
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;
}
多个功能区效果:
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该背景颜色
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>();
}
保留上次位置