Author Topic: BLOCKTOXREF Command  (Read 2404 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
BLOCKTOXREF Command
« on: November 30, 2011, 08:36:24 AM »
Does anyone have an example of how to use the BlockToXRef AutoCAD command? This is maybe a workaround to my other posting http://www.theswamp.org/index.php?topic=40103.0 that I haven't yet figured out. Not sure on how to write the blocktoxref command in .net and searches didn't turn up anything.

Thanks in advance,

Ted

BillZndl

  • Guest
Re: BLOCKTOXREF Command
« Reply #1 on: November 30, 2011, 09:41:43 AM »

kaefer

  • Guest
Re: BLOCKTOXREF Command
« Reply #2 on: November 30, 2011, 09:46:15 AM »
Does anyone have an example of how to use the BlockToXRef AutoCAD command?

Nope, I'll use my rudimentary capabilities in reading comprehension instead.

From acet.chm:
Quote
Replaces all instances of a standard block with an xref. Unbinds xrefs that are bound.

BLOCKTOXREF searches the entire drawing for references to a specified block name and replaces them with an xref that you specify. You can have the unreferenced block purged when done.

Disregarding the purging part this is exactly what this thread was all about. Still missing from the picture is any hint how you get your block definitions from your drawing on to the disk. The answer to that may be the WBLOCK command, whose .NET representation is unsurprisingly named Database.Wblock().

kaefer

  • Guest
Re: BLOCKTOXREF Command
« Reply #3 on: November 30, 2011, 11:55:03 AM »
Just for completeness sake, Here's a C# variant doing the complete routine:
1. Select a BlockReference
2. Wblock the BlockTableRecord (with SaveFileDialog)
3. OverlayXref
4. WblockCloneObjects to replace the existing BlockReferences

If the drawing is to stay open inside the drawing editor, there should a call to Database.ResolveXrefs() be thrown in.
Code: [Select]
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace Block2Xref
{
    public static class Block2Xref
    {
        [CommandMethod("MyBlock2Xref")]
        public static void Block2XrefCommand()
        {
            // The usual suspects
            Database db =
                acadApp.DocumentManager.MdiActiveDocument.Database;
            Editor ed =
                acadApp.DocumentManager.MdiActiveDocument.Editor;
            // Select BlockReference
            PromptEntityOptions peo =
                new PromptEntityOptions("Select BlockReference to convert to Xref:");
            peo.SetRejectMessage("Not a BlockReference.");
            peo.AddAllowedClass(typeof(BlockReference), false);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Get BlockTableRecord from reference and check for suitability
                BlockReference bref =
                    (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                ObjectId btrid =
                    bref.IsDynamicBlock ? bref.DynamicBlockTableRecord : bref.BlockTableRecord;
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(btrid, OpenMode.ForRead);
                if (btr.IsLayout ||
                    btr.IsAnonymous ||
                    btr.IsFromExternalReference ||
                    btr.IsFromOverlayReference)
                {
                    ed.WriteMessage("\nCannot convert block {0} to Xref. ", btr.Name);
                }
                else
                {
                    // Select file name to save BlockTableRecord
                    SaveFileDialog sfd =
                        new SaveFileDialog(
                            "Save BlockDefinition",
                            btr.Name,
                            "dwg",
                            "SaveBlockDefinitionDialog",
                            SaveFileDialog.SaveFileDialogFlags.NoFtpSites);
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        bool ok = false;
                        using (Database db1 = db.Wblock(btr.ObjectId))
                        {
                            try
                            {
                                db1.SaveAs(sfd.Filename, DwgVersion.Current);
                                ok = true;
                            }
                            catch (System.Exception ex)
                            {
                                ed.WriteMessage(
                                    "\nError writing file {0}: {1} ",
                                    sfd.Filename,
                                    ex.Message);
                            }
                        }
                        // Save successful?
                        if (ok)
                        {
                            // Overlay Xref in side database
                            using (Database db2 = new Database(true, false))
                            {
                                ObjectId xrid =
                                    db2.OverlayXref(sfd.Filename, btr.Name);
                                ObjectIdCollection oidc =
                                    new ObjectIdCollection(new[] { xrid });
                                // Clone BlockTableRecord into main database,
                                // overwriting the original in the process
                                db.WblockCloneObjects(
                                    oidc,
                                    db.BlockTableId,
                                    new IdMapping(),
                                    DuplicateRecordCloning.Replace,
                                    false);
                            }
                        }
                    }
                }
                tr.Commit();
            }
        }
    }
}
« Last Edit: November 30, 2011, 12:07:37 PM by kaefer »

TJK44

  • Guest
Re: BLOCKTOXREF Command
« Reply #4 on: November 30, 2011, 12:35:01 PM »
Thanks kaefer, I will play around with this code and see what I can do.

TJK44

  • Guest
Re: BLOCKTOXREF Command
« Reply #5 on: November 30, 2011, 03:38:06 PM »
That code actually helped me out a ton kaefer thanks a lot. I'm only having one issue and that is the wblocked files are being exploded. Does the wBlock command explode the blockreferences?

Code: [Select]
using (Database db1 = db.Wblock(btr.ObjectId))
                        {
                            try
                            {
                                db1.SaveAs(sfd.Filename, DwgVersion.Current);
                                ok = true;
                            }
                            catch (System.Exception ex)
                            {
                                ed.WriteMessage(
                                    "\nError writing file {0}: {1} ",
                                    sfd.Filename,
                                    ex.Message);
                            }
                         }             

Thanks,

Ted