using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using Ribbon.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace Ribbon
{
public class Command : IExtensionApplication
{
void IExtensionApplication.Initialize()
{
TAddRibbon();
}
public void Terminate()
{
throw new NotImplementedException();
}
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static ImageSource ImageSourceForBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally { DeleteObject(handle); }
}
private const string myTabId = "My Tool";
[CommandMethod("AddRibbon")]
public void TAddRibbon()
{
RibbonControl ribbonControl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
//RibbonControl ribbonControl = Autodesk.Windows.ComponentManager.Ribbon;
RibbonTab ribbonTab = new RibbonTab();
ribbonTab.Title = "Custom Ribbon";
ribbonTab.Id = myTabId;
ribbonControl.Tabs.Add(ribbonTab);
addPanelGetEntity(ribbonTab);
addPanelAddEntity(ribbonTab);
ribbonTab.IsActive = true;
}
private void addPanelGetEntity(RibbonTab ribbonTab)
{
RibbonPanelSource ribbonPanelSource = new RibbonPanelSource();
ribbonPanelSource.Title = "Select Entity";
RibbonPanel ribbonPanel = new RibbonPanel();
ribbonPanel.Source = ribbonPanelSource;
ribbonTab.Panels.Add(ribbonPanel);
RibbonButton ribbonButtonGetEntity = new RibbonButton();
ribbonButtonGetEntity.Text = "My Get Entity";
ribbonButtonGetEntity.ShowText = true;
ribbonButtonGetEntity.Image = ImageSourceForBitmap(Resource.smiley_16x16_png);
ribbonButtonGetEntity.LargeImage = ImageSourceForBitmap(Resource.smiley_32x32_png);
ribbonButtonGetEntity.ShowImage = true;
ribbonButtonGetEntity.Size = RibbonItemSize.Large;
ribbonButtonGetEntity.Orientation = System.Windows.Controls.Orientation.Horizontal;
ribbonButtonGetEntity.CommandParameter = "GetEntity ";
ribbonButtonGetEntity.CommandHandler = new AdskCommandHandler();
ribbonPanelSource.Items.Add(ribbonButtonGetEntity);
}
private void addPanelAddEntity(RibbonTab ribbonTab)
{
RibbonPanelSource ribbonPanelSource = new RibbonPanelSource();
ribbonPanelSource.Title = "Add Entity";
RibbonPanel ribbonPanel = new RibbonPanel();
ribbonPanel.Source = ribbonPanelSource;
ribbonTab.Panels.Add(ribbonPanel);
RibbonButton ribbonButtonAddLine = new RibbonButton();
ribbonButtonAddLine.Text = "My Add Line";
ribbonButtonAddLine.ShowText = true;
ribbonButtonAddLine.CommandParameter = "AddLine ";
ribbonButtonAddLine.CommandHandler = new AdskCommandHandler();
ribbonPanelSource.Items.Add(ribbonButtonAddLine);
}
[CommandMethod("AddLine")]
public void HDDL()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Line Lin = new Line();
Lin.StartPoint = new Point3d(10, 20, 0);
Lin.EndPoint = new Point3d(20, 20, 0);
Transaction acTrans = db.TransactionManager.StartTransaction();
using (acTrans)
{
BlockTable bt = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(Lin);
acTrans.AddNewlyCreatedDBObject(Lin, true);
acTrans.Commit();
}
}
[CommandMethod("GetEntity")]
public static void TGetEntity()
{
Document AcDoc = AcAp.DocumentManager.MdiActiveDocument;
Editor AcEd = AcDoc.Editor;
PromptEntityOptions pEntOpts = new PromptEntityOptions("\nSelect entity: ");
pEntOpts.AllowNone = true;
PromptEntityResult pEntRes = AcEd.GetEntity(pEntOpts);
if (pEntRes.Status == PromptStatus.OK)
{
AcEd.WriteMessage("\nEnity selected is: " + pEntRes.ObjectId.ObjectClass.DxfName + ".");
}
else
{
AcEd.WriteMessage("\nThere is no entity selected.");
}
}
}
}