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,动手编起来倒挺快,就几行代码)
源代码如下:

using System ;
using System.IO;
using System.Text;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.PlottingServices;

[assembly: CommandClass(
typeof(ClassLibrary.Road))]

namespace ClassLibrary
{
    
/// <summary>
    
/// Summary description for Class.
    
/// </summary>

    public class Road
    
{

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


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

            
else
            
{
                
return;
            }

            StreamReader sr 
= new StreamReader(filename);
            
            Transaction transaction
=null;
            
string data = sr.ReadLine();
            
if(data==null)
                
return;
            
string[] datas = data.Split(',');
            
int no = int.Parse(datas[0]);
            
double x = double.Parse(datas[1]);
            
double y = double.Parse(datas[2]);
            Point3d p1 
= new Point3d(x,y,0);
            data 
= sr.ReadLine();
            
while(data!=null)
            
{
                
if(data=="")
                    
continue;
                datas 
= data.Split(',');
                no 
= int.Parse(datas[0]);
                x 
= double.Parse(datas[1]);
                y 
= double.Parse(datas[2]);
                Point3d p2 
= new Point3d(x,y,0);
                Line line 
= new Line(p1,p2);

                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
                BlockTable bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
                
                btr.AppendEntity(line);
                transaction.AddNewlyCreatedDBObject(line,
true);
                transaction.Commit();

                p1 
= p2;
                data 
= sr.ReadLine();
            }

        }


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

            
else
            
{
                
return;
            }

            StreamReader sr 
= new StreamReader(filename);
            
            
double baseHeight = 0;    
            
string data = sr.ReadLine();
            Transaction transaction
=null;
            
while(data!=null)
            
{
                
if(data=="")
                    
continue;
                
string[] datas = data.Split(',');
                
int no = int.Parse(datas[0]);
                
double stake = double.Parse(datas[1]);
                
string left = sr.ReadLine();
                
string right = sr.ReadLine();
                
string[] lefts = left.Split(',');
                
string[] rights = right.Split(',');

                Point3d center 
= new Point3d(0,baseHeight,0);
                Point3d lp1 
= new Point3d(-double.Parse(lefts[0]),double.Parse(lefts[1])+baseHeight,0);
                Point3d lp2 
= new Point3d(-double.Parse(lefts[2]),double.Parse(lefts[3])+baseHeight,0);
                Point3d rp1 
= new Point3d(double.Parse(rights[0]),double.Parse(rights[1])+baseHeight,0);
                Point3d rp2 
= new Point3d(double.Parse(rights[2]),double.Parse(rights[3])+baseHeight,0);

                Line l1 
= new Line(center,lp1);
                Line r1 
= new Line(center,rp1);
                Line r2 
= new Line(rp1,rp2);
                Line l2 
= new Line(lp1,lp2);
                
                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
                BlockTable bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
                
                btr.AppendEntity(l1);
                transaction.AddNewlyCreatedDBObject(l1,
true);
                transaction.Commit();
                
                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
                
                btr.AppendEntity(l2);
                transaction.AddNewlyCreatedDBObject(l2,
true);
                transaction.Commit();

                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;

                btr.AppendEntity(r1);
                transaction.AddNewlyCreatedDBObject(r1,
true);
                transaction.Commit();
                

                transaction 
= HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
                bt 
= (BlockTable)transaction.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
                btr 
= transaction.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;

                btr.AppendEntity(r2);
                transaction.AddNewlyCreatedDBObject(r2,
true);
                transaction.Commit();

                baseHeight 
+= 10;
                data 
= sr.ReadLine();

            }

        }

    }

}




 

原文地址:https://www.cnblogs.com/xiexiaokui/p/302201.html