本人最近刚入手学习c#开发autocad,正找到志同道合的朋友一起来学,并且希望可以帮朋友们做一些简单的设计,希望大家能提一些宝贵的建议啊!
本人最近刚入手学习c#开发autocad,正找到志同道合的朋友一起来学,并且希望可以帮朋友们做一些简单的设计,希望大家能提一些宝贵的建议啊!
2楼
下面是我做的几个小例子:圆的填充,定点填充和直线裁剪
[CommandMethod("try1")]
//圆的填充
public void ttt()
{
Point3d center = new Point3d(10, 10,0);
Circle circle = new Circle(center, Vector3d.ZAxis, 50);
//BulgeVertex bv=new BulgeVertex(new Point2d(10,10),0);
//bv.Equals(circle);
//HatchLoop hl=new HatchLoop();
//hl.Add(bv);
Hatch myha=new Hatch();
myha.PatternScale=1;
myha.SetHatchPattern(HatchPatternType.PreDefined,"HEX");
//myha.HatchStyle=HatchStyle.Normal;
ObjectIdCollection obs=new ObjectIdCollection();
ObjectId id = AddEntity(circle);
obs.Add(id);
myha.AppendLoop(0,obs);
myha.EvaluateHatch(true);
//ObjectId id = AddEntity(circle);
ObjectId id1 = AddEntity(myha);
}
[CommandMethod("try2")]
//三点填充
public void ttt2()
{
BulgeVertex bv1=new BulgeVertex(new Point2d(10,10),0);
BulgeVertex bv2=new BulgeVertex(new Point2d(30,10),0);
BulgeVertex bv3=new BulgeVertex(new Point2d(30,30),0);
BulgeVertex bv4=new BulgeVertex(new Point2d(10,10),0);
HatchLoop hl=new HatchLoop();
hl.Add(bv1);
hl.Add(bv2);
hl.Add(bv3);
hl.Add(bv4);
Hatch myha=new Hatch();
myha.PatternScale=0.2;
myha.SetHatchPattern(HatchPatternType.PreDefined,"CROSS");
myha.AppendLoop(hl);
myha.EvaluateHatch(true);
ObjectId id1 = AddEntity(myha);
}
[CommandMethod("try3")]
public void ttt3()
//直线裁剪
{
Line l1=new Line();
l1.StartPoint=new Point3d(10,10,0);
l1.EndPoint=new Point3d(100,100,0);
Line l2=new Line();
l2.StartPoint=new Point3d(10,100,0);
l2.EndPoint=new Point3d(100,10,0);
Point3dCollection p1=new Point3dCollection();
l1.IntersectWith(l2,Intersect.ExtendArgument,p1,0,0);
Point3d pt=new Point3d(p1[0].X,p1[0].Y,p1[0].Z);
l1.EndPoint=pt;
ObjectId ob=AddEntity(l1);
ObjectId ob2=AddEntity(l2);
}
public static ObjectId AddEntity(Entity ent)
{Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
Database Db=HostApplicationServices.WorkingDatabase;
try
{
BlockTable bt =
(BlockTable)trans.GetObject(Db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord btr =
(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false);
btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
return ent.ObjectId;
}
catch (System.Exception e)
{
throw e;
}
}
回复
3楼
我希望这个帖子能继续下去,也希望这个帖子能成为cad应用的经典帖子,楼主好好努力!
谢谢对本站的支持!
回复
4楼
今天学习如何在autocad.net中右键弹出一个菜单并使用circle命令画圆.
autocad中做菜单用到ContextMenuExtension,我们可以新构件一个ContextMenuExtension类(用过C++的朋友应该很熟悉类的构造方法)并做设置,用来实现某些功能!
public class MyContextMenu : ContextMenuExtension
{
private MenuItem m_main = new MenuItem("MAIN MENU");
private MenuItem m_sub0 = new MenuItem("SUBMENU 0");
private MenuItem m_sub1 = new MenuItem("SUBMENU 1");
private MenuItem m_sub2 = new MenuItem("SUBMENU 2");
static public bool m_bShowExtraMenus = true;
public MyContextMenu()
{
this.Title = "Test";//菜单名称
this.MenuItems.Add(m_main);
m_main.MenuItems.Add(m_sub0);//弹出式PopupMenu名称
m_main.MenuItems.Add(m_sub1);
this.Popup += new EventHandler(OnUpdateMenu);//主菜单响应事件
m_sub0.Click += new EventHandler(OnClick);
m_sub1.Click += new EventHandler(OnClick);
m_sub2.Click += new EventHandler(OnClick);
}
void OnClick(object sender, EventArgs e)
{
}
void OnUpdateMenu(object sender, EventArgs e)
{
//开始不加m_sub2,发现没有加上,有了减去
if (MyContextMenu.m_bShowExtraMenus)
{
if (!m_main.MenuItems.Contains(m_sub2))
{
m_main.MenuItems.Add(m_sub2);
}
}
else
{
if (m_main.MenuItems.Contains(m_sub2))
{
m_main.MenuItems.Remove(m_sub2);
}
}
MyContextMenu.m_bShowExtraMenus =!MyContextMenu.m_bShowExtraMenus;
}
}
构件好这个类后就可以使用了:
[CommandMethod("TEST")]
public static void DoIt()
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("注册一个画圆的菜单!\n");
MyContextMenu cme = new MyContextMenu();
Application.AddObjectContextMenuExtension(RXObject.GetClass(typeof
(Circle)), cme);
}
}
回复
5楼
如何在autocad中插入已知文件的块?
假设我们想插入c:\aaa.dwg 中一个块"my_block";
[CommandMethod("testinsert")]
static public void DoIt()
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//定义文档
string fname = @"C:\aaa.dwg";
try
{
if ( ! File.Exists(fname))
fname = HostApplicationServices.Current.FindFile(fname,doc.Database,FindFileHint.Default);
Database db = new Database(false, false);
db.ReadDwgFile(fname, FileShare.Read, true, null);//读文件
Transaction t =doc.TransactionManager.StartTransaction();
ObjectId idBTR = doc.Database.Insert("my_block",db,false);
//数据库中插入我们定义的块
BlockTable bt =(BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr =(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
BlockReference bref = new BlockReference(Point3d.Origin, idBTR);
//以上定义块,块记录和索引
btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);
t.Commit();
}
finally
{t.Dispose();
}
}
回复
6楼
我曾经学过 但感觉太枯燥 而且可视化不好 放弃了
希望你能够坚持下去 支持你
回复
7楼
我支持楼上写程序的朋友。
谢谢大家了!
回复
8楼
看不懂.....只会lsp,不过支持一下,写程序是个艰苦的过程,要坚持哈....
回复
9楼
谢谢大家,我也是初学者。感觉它的二次开发没有想象中困难,更多是基于C#的操作,希望能有更多爱好编程的朋友用于实践。
回复
10楼
顶一下老帖,希望大家能参考
回复
11楼
高人呀,我都没看懂.不过还是顶一下,这么辛苦写出来了,希望能对有需要者有所帮助.
回复