Autodesk CAD实体集合另存为小工具

Autodesk CAD实体集合另存为小工具

一,使用Autodesk CAD的二次开发技术ObjectARX(依赖与Autodesk CAD)。其中有c++和C#版,这里使用C#版。

二,生成的dll(C#)或arx(c++),必须被拷贝到ACAD的目录下,且在ACAD中使用命令netload或appload加载生成的dll或arx,加载后我们在ACAD中输入我们的命令即可。(下面小程序的命令是SaveDwg

三,小程序的功能是选取ACAD中打开的dwg图纸上的一些内容,然后保存为另一张dwg图纸。

四,c#的代码如下,比较简单,只考虑了简单的情况。

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Windows.Forms;
using  System.Collections;

using  Autodesk.AutoCAD.Runtime;
using  Autodesk.AutoCAD.DatabaseServices;
using  Autodesk.AutoCAD.EditorInput;
using  Autodesk.AutoCAD.ApplicationServices;

namespace  SaveDwg
{
    
public class Program
    
{   
 
        [CommandMethod(
"SaveDwg")]   
        
        
public void SaveDwg()
        
{
            
try
            
{

                Database db 
= HostApplicationServices.WorkingDatabase;

                SelectionSet selectionset;
                ArrayList setList 
= new ArrayList();

                Editor ed 
= Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage(
"welcome to use the tool, it can save as a dwg file for your selected entities.\n");
                PromptSelectionOptions selectionOptions 
= new PromptSelectionOptions();
                PromptSelectionResult selectionResult;
                selectionResult 
= ed.GetSelection(selectionOptions);
                
if (selectionResult.Status != PromptStatus.OK)
                
{
                    ed.WriteMessage(
"select error, please use command again.\n");
                    
return;
                }

                
else
                
{
                    selectionset 
= selectionResult.Value;
                    
foreach (ObjectId id in selectionset.GetObjectIds())
                    
{
                        DBObject ob 
= id.Open(OpenMode.ForRead);
                        Entity ent 
= ob as Entity;
                        
if (ent != null)
                        
{
                            Entity ent2 
= ent.Clone() as Entity;
                            setList.Add(ent2);
                            ed.WriteMessage(
"copying entity is successful.\n");
                        }
                        
                        ob.Close();
                    }

                }

                ed.WriteMessage(
"select and copy successfully.\n");

                Database Db 
= new Database(truefalse);
                HostApplicationServices.WorkingDatabase 
= Db;
                Autodesk.AutoCAD.DatabaseServices.TransactionManager manager 
= Db.TransactionManager;
                
using (Transaction transaction = manager.StartTransaction())
                
{
                    BlockTable table 
= (BlockTable)manager.GetObject(Db.BlockTableId, OpenMode.ForWrite);
                    BlockTableRecord record 
= (BlockTableRecord)manager.GetObject(Db.CurrentSpaceId, OpenMode.ForWrite);

                    
foreach (Object obj in setList)
                    
{
                        Entity ent 
= obj as Entity;
                        
if (ent != null)
                        
{
                            record.AppendEntity(ent);
                            
//manager.AddNewlyCreatedDBObject(ent,true);
                        }


                    }

                    record.Close();
                    table.Close();
                    transaction.Commit();
                }


                SaveFileDialog dialog 
= new SaveFileDialog();
                
string filePath;
                
if (DialogResult.OK == dialog.ShowDialog())
                
{
                    filePath 
= dialog.FileName;
                    Db.SaveAs(filePath, DwgVersion.Current);
                }

                
            }

            
catch(System.Exception e)
            
{
                MessageBox.Show(
"copy fail");
            }

        }

    }

}


五,记得要reference ObjectARX程序需要的dll哦,如下图:
Autodesk CAD实体集合另存为小工具_第1张图片
六,想要脱离AutodeskCAD处理dwg图纸,需要使用ObjectDBX技术,最新版改名为realdwg技术。

你可能感兴趣的:(Autodesk CAD实体集合另存为小工具)