Exporting multiple blocks in one dwg to having them as individual dwgs

Exporting multiple blocks in one dwg to having them as individual dwgs

Anonymous
Not applicable
1,363 Views
2 Replies
Message 1 of 3

Exporting multiple blocks in one dwg to having them as individual dwgs

Anonymous
Not applicable

there are some blocks in one dwg file,how can i exporting them as individual dwgs,

 

please show me the code,if you like,

 

thank you

 

0 Likes
Accepted solutions (1)
1,364 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

This is some cun&paste from old code, so forgive me if I left some unreferenced functions, but you should be able to overcome them:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

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

using WIN = System.Windows.Forms;

[assembly: CommandClass(typeof(WorkshopNET.csBlockTools))]

namespace WorkshopNET
{
    public class csBlockTools
    {

       [CommandMethod("export_blocks", CommandFlags.UsePickSet | CommandFlags.Redraw | CommandFlags.Modal)]
        static public void comsal_export_blocks()
        {
            if (Application.DocumentManager.Count == 0) return;

            // shorter references
            Document doc = Application.DocumentManager.MdiActiveDocument;
            if (doc == null) return;
            DocumentLock docLock = doc.LockDocument(DocumentLockMode.AutoWrite, "export_blocks", "export_blocks", true);
            Database dwg = doc.Database;
            Editor ed = doc.Editor;

            // ask to select blocks to export, it works also with pre-selected entities
            PromptSelectionResult selectionRes = ed.SelectImplied();

            // Check for pre-selected entities...
            if (selectionRes.Status == PromptStatus.Error)
            {
                // ... otherwise ask for selection
                PromptSelectionOptions opt_sel = new PromptSelectionOptions();
                opt_sel.MessageForAdding = "\nSelect blocks to export:";
                opt_sel.MessageForRemoval = "\nUnselect blocks:";

                TypedValue[] tvsb = new TypedValue[1] 
                { 
                    new TypedValue((int)DxfCode.Start, "INSERT") 
                };

                SelectionFilter sf = new SelectionFilter(tvsb);

                selectionRes = ed.GetSelection(opt_sel, sf);
                if (selectionRes.Status != PromptStatus.OK) return;     // Exit if abort
            }
            else
            {
                // Clean pre-selection
                ed.SetImpliedSelection(new ObjectId[0]);
            }

            if (selectionRes.Status != PromptStatus.OK) return;
            if (selectionRes.Value.Count == 0) return;

            // Ask for target directory
            // as default use same folder of the current document
            DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(doc.Name));

            // use windows standard dialog
            WIN.FolderBrowserDialog fbd = new WIN.FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;
            fbd.SelectedPath = d.FullName;

            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                String szBaseFolder = fbd.SelectedPath + "\\";     // shorter reference

                Transaction tr = dwg.TransactionManager.StartTransaction();

                try
                {
                    // cycle on blocks and export them
                    foreach (SelectedObject ss in selectionRes.Value)
                    {
                        DBObject dbo = tr.GetObject(ss.ObjectId, OpenMode.ForRead);
                        if (dbo is BlockReference && !dbo.IsErased)
                        {
                            BlockReference br = dbo as BlockReference;
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(br.IsDynamicBlock ? br.DynamicBlockTableRecord : br.BlockTableRecord, OpenMode.ForRead);

                            if ((btr.Name.Length > 0) && (!btr.Name.Contains("*")))
                            {
                                String szNewName = szBaseFolder + btr.Name + @".dwg";

                                // build a new database and save it as WBLOCK()
                                Database newdb = new Database(false, true);
                                newdb = doc.Database.Wblock(btr.ObjectId);
                                newdb.SaveAs(szNewName, DwgVersion.Current);
                                newdb.Dispose();
                            }
                        }
                    }

                    tr.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("\n*** unexpected error : " + ex.Message + " ***");
                }
                finally
                {
                    tr.Dispose();
                }
            }

            ed.WriteMessage("\n");
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
}
Message 3 of 3

Anonymous
Not applicable
thank you very much,i'll test it.thank you your quick reply.
0 Likes