Skip to content

Commit 9ab0b7d

Browse files
committed
Add support ribbon cad and civil 3d
1 parent 427fd4b commit 9ab0b7d

File tree

8 files changed

+182
-2
lines changed

8 files changed

+182
-2
lines changed

CADPythonShell/CADPythonShell.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
<Reference Include="acdbmgd">
9595
<HintPath>C:\Program Files\Autodesk\AutoCAD 2022\acdbmgd.dll</HintPath>
9696
</Reference>
97+
<Reference Include="AdWindows">
98+
<HintPath>C:\Program Files\Autodesk\AutoCAD 2022\AdWindows.dll</HintPath>
99+
</Reference>
97100
<Reference Include="ICSharpCode.AvalonEdit, Version=5.0.3.0, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
98101
<SpecificVersion>False</SpecificVersion>
99102
<HintPath>..\RefrencedAssemblies\ICSharpCode.AvalonEdit.dll</HintPath>
@@ -168,6 +171,7 @@
168171
<Compile Include="IronPythonConsole.xaml.cs">
169172
<DependentUpon>IronPythonConsole.xaml</DependentUpon>
170173
</Compile>
174+
<Compile Include="IronPythonConsoleApp.cs" />
171175
<Compile Include="IronPythonConsoleCommand.cs" />
172176
<Compile Include="Properties\AssemblyInfo.cs" />
173177
<Compile Include="Properties\Resources.Designer.cs">
@@ -176,6 +180,7 @@
176180
<DependentUpon>Resources.resx</DependentUpon>
177181
</Compile>
178182
<Compile Include="CADPythonShellApplication.cs" />
183+
<Compile Include="RelayCommand.cs" />
179184
</ItemGroup>
180185
<ItemGroup>
181186
<EmbeddedResource Include="ConfigureCommandsForm.resx">

CADPythonShell/CADPythonShellApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static ImageSource GetEmbeddedBmp(System.Reflection.Assembly app, string
6868
return source.Frames[0];
6969
}
7070

71-
private static ImageSource GetEmbeddedPng(System.Reflection.Assembly app, string imageName)
71+
public static ImageSource GetEmbeddedPng(System.Reflection.Assembly app, string imageName)
7272
{
7373
var file = app.GetManifestResourceStream(imageName);
7474
var source = PngBitmapDecoder.Create(file, BitmapCreateOptions.None, BitmapCacheOption.None);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Controls;
7+
using System.Windows.Input;
8+
using System.Windows.Media.Imaging;
9+
using Autodesk.AutoCAD.Runtime;
10+
using Autodesk.Windows;
11+
12+
namespace CADPythonShell
13+
{
14+
public class IronPythonConsoleApp
15+
{
16+
[CommandMethod("InitPythonConsole")]
17+
public void Execute()
18+
{
19+
CreateRibbon();
20+
}
21+
void CreateRibbon()
22+
{
23+
RibbonControl ribbon = ComponentManager.Ribbon;
24+
if (ribbon != null)
25+
{
26+
RibbonTab rtab = ribbon.FindTab("PythonShell");
27+
if (rtab != null)
28+
{
29+
ribbon.Tabs.Remove(rtab);
30+
}
31+
rtab = new RibbonTab();
32+
rtab.Title = "Python Shell";
33+
rtab.Id = "PythonShell";
34+
//Add the Tab
35+
ribbon.Tabs.Add(rtab);
36+
addContent(rtab);
37+
}
38+
}
39+
private void addContent(RibbonTab rtab)
40+
{
41+
rtab.Panels.Add(AddOnePanel());
42+
}
43+
static RibbonPanel AddOnePanel()
44+
{
45+
//https://forums.autodesk.com/t5/net/create-custom-ribbon-tab-and-buttons-for-autocad-mechanical-2011/td-p/2834343
46+
RibbonPanelSource rps = new RibbonPanelSource();
47+
rps.Title = "Autocad Python Shell";
48+
RibbonPanel rp = new RibbonPanel();
49+
rp.Source = rps;
50+
//Create a Command Item that the Dialog Launcher can use,
51+
// for this test it is just a place holder.
52+
RibbonButton rci = new RibbonButton();
53+
rci.Name = "Python Shell Console";
54+
rps.DialogLauncher = rci;
55+
//create button1
56+
RibbonButton rb = new RibbonButton();
57+
rb.Orientation = Orientation.Vertical;
58+
rb.AllowInStatusBar = true;
59+
rb.Size = RibbonItemSize.Large;
60+
rb.Name = "Run APS";
61+
rb.ShowText = true;
62+
rb.Text = "Run APS";
63+
var addinAssembly = typeof(IronPythonConsoleApp).Assembly;
64+
rb.Image = CADPythonShellApplication.GetEmbeddedPng(addinAssembly, "CADPythonShell.Resources.Python-16.png");
65+
rb.LargeImage = CADPythonShellApplication.GetEmbeddedPng(addinAssembly, "CADPythonShell.Resources.Python-32.png");
66+
rb.CommandHandler = new RelayCommand(new IronPythonConsoleCommand().Execute);
67+
rps.Items.Add(rb);
68+
//create button2
69+
RibbonButton rb2 = new RibbonButton();
70+
rb2.Orientation = Orientation.Vertical;
71+
rb2.AllowInStatusBar = true;
72+
rb2.Size = RibbonItemSize.Large;
73+
rb2.Name = "Configure APS";
74+
rb2.ShowText = true;
75+
rb2.Text = "Configure APS";
76+
rb2.Image = CADPythonShellApplication.GetEmbeddedPng(addinAssembly, "CADPythonShell.Resources.Settings-16.png");
77+
rb2.LargeImage = CADPythonShellApplication.GetEmbeddedPng(addinAssembly, "CADPythonShell.Resources.Settings-32.png");
78+
79+
rb2.CommandHandler = new RelayCommand(new ConfigureCommand().Execute);
80+
//Add the Button to the Tab
81+
rps.Items.Add(rb2);
82+
return rp;
83+
}
84+
}
85+
}

CADPythonShell/IronPythonConsoleCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Windows.Threading;
66
using Autodesk.AutoCAD.ApplicationServices;
77
using Autodesk.AutoCAD.Runtime;
8+
using Autodesk.Windows;
89
using CADRuntime;
910
using Microsoft.Scripting;
1011
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
@@ -93,5 +94,7 @@ public void Execute()
9394
helper.Owner = hander;
9495
gui.Show();
9596
}
96-
}
97+
98+
99+
}
97100
}

CADPythonShell/RelayCommand.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Input;
4+
5+
namespace CADPythonShell
6+
{
7+
/// <summary>
8+
/// A general relay command that takes its parameter as an object
9+
/// </summary>
10+
public class RelayCommand : ICommand
11+
{
12+
#region Variables
13+
private readonly Predicate<object> m_canExecute;
14+
private readonly Action<object> m_execute;
15+
private readonly Action _act;
16+
17+
#endregion
18+
19+
#region Constructor
20+
public RelayCommand(Action act)
21+
{
22+
_act = act;
23+
}
24+
25+
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
26+
{
27+
if (execute == null)
28+
{
29+
throw new ArgumentNullException("Execute");
30+
}
31+
m_execute = execute;
32+
m_canExecute = canExecute;
33+
}
34+
35+
#endregion
36+
37+
#region Implementation
38+
// Evaluate the command if it is valid to execute
39+
public bool CanExecute(object parameter = null)
40+
{
41+
if (parameter == null || m_canExecute == null) return true;
42+
else return m_canExecute(parameter);
43+
}
44+
// Main execute method
45+
public void Execute(object parameter = null)
46+
{
47+
if (_act != null) _act();
48+
else m_execute(parameter);
49+
}
50+
51+
// In WPF CommandManager is a pre-defined class that take charge of observing the user interface
52+
// and calls the CanExecute method when it deems it necessary
53+
public event EventHandler CanExecuteChanged
54+
{
55+
add => CommandManager.RequerySuggested += value;
56+
remove => CommandManager.RequerySuggested -= value;
57+
}
58+
#endregion
59+
60+
#region Support
61+
public class CloseCommand : ICommand
62+
{
63+
public bool CanExecute(object parameter)
64+
{
65+
return true;
66+
}
67+
68+
public event EventHandler CanExecuteChanged
69+
{
70+
add => CommandManager.RequerySuggested += value;
71+
remove => CommandManager.RequerySuggested -= value;
72+
}
73+
74+
public void Execute(object parameter)
75+
{
76+
Window myWin = parameter as Window;
77+
myWin.Close();
78+
}
79+
}
80+
#endregion
81+
}
82+
}
83+

Images/Ribbon.png

13.6 KB
Loading

PackageContents.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Commands>
1717
<Command Local="PythonConsole" Global="PythonConsole" />
1818
<Command Local="PythonShellSetting" Global="PythonShellSetting" />
19+
<Command Local="InitPythonConsole" Global="InitPythonConsole" StartupCommand="True" />
1920
</Commands>
2021
</ComponentEntry>
2122
</Components>

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ and it would not be possible without the great work of everyone involved with th
1919
The CADPythonShell (CPS) ,lets you to write plugins for Autocad in Python, provides you with an interactive shell that lets you see the results of your code *as you type it*. This is great for exploring the Autocad API.
2020

2121
The biggest limitation is that you can't deploy DLLs with custom scripts at this time and you can't subscribe to events at startup time.
22+
23+
![](Images/Ribbon.png)
24+
2225
## Features
2326

2427
- Interactive IronPython interpreter for exploring the API

0 commit comments

Comments
 (0)