AutoCAD2006+c#+ObjectArx简单纵横断面程序

    应老同学的要求,用C#+ObjectArx编了第一个程序,功能是根据测量的数据文件,测绘纵横断面图。
    纵断面数据示例:
1,0,3.9
2,10,3.87
3,20,3.86
4,21.5,2.4
5,40,2.42
    横断面数据示例:
0,3.9
5,3.92,10,3.97
5,3.93,10,3.98
20,3.86
5,3.85,10,3.85
5,3.86,10,3.84
40,2.42
5,2.41,10,2.43
5,2.43,10,2.44
花了一个晚上(找资料,熟悉ObjectArx,动手编起来倒挺快,就几行代码)
源代码如下:

None.gif using  System ;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  Autodesk.AutoCAD.Runtime ;
None.gif
using  Autodesk.AutoCAD.ApplicationServices;
None.gif
using  Autodesk.AutoCAD.EditorInput;
None.gif
using  Autodesk.AutoCAD.Geometry;
None.gif
using  Autodesk.AutoCAD.DatabaseServices;
None.gif
using  Autodesk.AutoCAD.Windows;
None.gif
using  Autodesk.AutoCAD.GraphicsInterface;
None.gif
using  Autodesk.AutoCAD.Colors;
None.gif
using  Autodesk.AutoCAD.PlottingServices;
None.gif
None.gif[assembly: CommandClass(
typeof (ClassLibrary.Road))]
None.gif
None.gif
namespace  ClassLibrary
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Class.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Road
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
// Define Command "AsdkCmd1"
InBlock.gif
        [CommandMethod("Road")]
InBlock.gif        
static public void Help()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string s = "道路绘图程序\n程序设计:风\n命令列表\n横断面:Transect,纵断面:Vertical\n技术支持:http://xiexiaokui.cnblogs.com\nQQ:57164718";
InBlock.gif            System.Windows.Forms.MessageBox.Show(s);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [CommandMethod(
"Vertical")]
InBlock.gif        
static public void Vertical()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string filename;
InBlock.gif            Autodesk.AutoCAD.Windows.OpenFileDialog ofd 
= new OpenFileDialog("纵断面数据","","txt","打开纵断面数据",OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
InBlock.gif            
if(ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                filename 
= ofd.Filename;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            StreamReader sr 
= new StreamReader(filename);
InBlock.gif            
InBlock.gif            Transaction transaction
=null;
InBlock.gif            
string data = sr.ReadLine();
InBlock.gif            
if(data==null)
InBlock.gif                
return;
InBlock.gif            
string[] datas = data.Split(',');
InBlock.gif            
int no = int.Parse(datas[0]);
InBlock.gif            
double x = double.Parse(datas[1]);
InBlock.gif            
double y = double.Parse(datas[2]);
InBlock.gif            Point3d p1 
= new Point3d(x,y,0);
InBlock.gif            data 
= sr.ReadLine();
InBlock.gif            
while(data!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(data=="")
InBlock.gif                    
continue;
InBlock.gif                datas 
= data.Split(',');
InBlock.gif                no 
= int.Parse(datas[0]);
InBlock.gif                x 
= double.Parse(datas[1]);
InBlock.gif                y 
= double.Parse(datas[2]);
InBlock.gif                Point3d p2 
= new Point3d(x,y,0);
InBlock.gif                Line line 
= new Line(p1,p2);
InBlock.gif
InBlock.gif                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
InBlock.gif                BlockTable bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
InBlock.gif                BlockTableRecord btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
InBlock.gif                
InBlock.gif                btr.AppendEntity(line);
InBlock.gif                transaction.AddNewlyCreatedDBObject(line,
true);
InBlock.gif                transaction.Commit();
InBlock.gif
InBlock.gif                p1 
= p2;
InBlock.gif                data 
= sr.ReadLine();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
InBlock.gif        
// Define Command "AsdkCmd1"
InBlock.gif
        [CommandMethod("Transect")]
InBlock.gif        
static public void Transect() // This method can have any name
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
string filename;
InBlock.gif            Autodesk.AutoCAD.Windows.OpenFileDialog ofd 
= new OpenFileDialog("纵断面数据","","txt","打开纵断面数据",OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
InBlock.gif            
if(ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                filename 
= ofd.Filename;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            StreamReader sr 
= new StreamReader(filename);
InBlock.gif            
InBlock.gif            
double baseHeight = 0;    
InBlock.gif            
string data = sr.ReadLine();
InBlock.gif            Transaction transaction
=null;
InBlock.gif            
while(data!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(data=="")
InBlock.gif                    
continue;
InBlock.gif                
string[] datas = data.Split(',');
InBlock.gif                
int no = int.Parse(datas[0]);
InBlock.gif                
double stake = double.Parse(datas[1]);
InBlock.gif                
string left = sr.ReadLine();
InBlock.gif                
string right = sr.ReadLine();
InBlock.gif                
string[] lefts = left.Split(',');
InBlock.gif                
string[] rights = right.Split(',');
InBlock.gif
InBlock.gif                Point3d center 
= new Point3d(0,baseHeight,0);
InBlock.gif                Point3d lp1 
= new Point3d(-double.Parse(lefts[0]),double.Parse(lefts[1])+baseHeight,0);
InBlock.gif                Point3d lp2 
= new Point3d(-double.Parse(lefts[2]),double.Parse(lefts[3])+baseHeight,0);
InBlock.gif                Point3d rp1 
= new Point3d(double.Parse(rights[0]),double.Parse(rights[1])+baseHeight,0);
InBlock.gif                Point3d rp2 
= new Point3d(double.Parse(rights[2]),double.Parse(rights[3])+baseHeight,0);
InBlock.gif
InBlock.gif                Line l1 
= new Line(center,lp1);
InBlock.gif                Line r1 
= new Line(center,rp1);
InBlock.gif                Line r2 
= new Line(rp1,rp2);
InBlock.gif                Line l2 
= new Line(lp1,lp2);
InBlock.gif                
InBlock.gif                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
InBlock.gif                BlockTable bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
InBlock.gif                BlockTableRecord btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
InBlock.gif                
InBlock.gif                btr.AppendEntity(l1);
InBlock.gif                transaction.AddNewlyCreatedDBObject(l1,
true);
InBlock.gif                transaction.Commit();
InBlock.gif                
InBlock.gif                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
InBlock.gif                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
InBlock.gif                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
InBlock.gif                
InBlock.gif                btr.AppendEntity(l2);
InBlock.gif                transaction.AddNewlyCreatedDBObject(l2,
true);
InBlock.gif                transaction.Commit();
InBlock.gif
InBlock.gif                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
InBlock.gif                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
InBlock.gif                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
InBlock.gif
InBlock.gif                btr.AppendEntity(r1);
InBlock.gif                transaction.AddNewlyCreatedDBObject(r1,
true);
InBlock.gif                transaction.Commit();
InBlock.gif                
InBlock.gif
InBlock.gif                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
InBlock.gif                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
InBlock.gif                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
InBlock.gif
InBlock.gif                btr.AppendEntity(r2);
InBlock.gif                transaction.AddNewlyCreatedDBObject(r2,
true);
InBlock.gif                transaction.Commit();
InBlock.gif
InBlock.gif                baseHeight 
+= 10;
InBlock.gif                data 
= sr.ReadLine();
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}




 

转载于:https://www.cnblogs.com/xiexiaokui/archive/2005/12/22/302201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值