Jump to content

Reading External DWG File using AutoCAD .net api


Recommended Posts

Posted

Reading the blocks from External files using AutoCAD .NET API with c# I can read the block References using  ReadDwgFile() but I am not getting the properties of the block if it is a dynamic block I am not getting the value of Visibility property instead I am getting the default value. can anyone help me with this issue?

Posted

I've barley started with C# and AutoCAD, so I am probably off target, but dealing with Dynamic Blocks in VLISP is totally different than getting regular (nondynamic) block properties.

 

AFAIK it is the same with C#,  VBA, .NET, etc.

 

You'll need to reference something like GetDynamicBlockVisibility If my previous research wasn't faulty.

Posted

If you have a look at Lee-mac.com, dynamic block lisp, yes it is lisp but he has posted numerous functions to do with dynamic blocks may give you some clues and how to write an equivalent C# version. 

 

 

  • 2 weeks later...
Posted (edited)

I have a dynamic block with 5 visibilities. I have placed each instance of the dynamic block with different Visibility so I have 5 blocks in a dwg file but when I try to read this external dwg file using the ReadDwgFile() method I am getting the Anonyms blocks for the remaining 4 instances other than the default Visibility block, how to get the original block name while reading from the External dwg file. Still, when I am reading through the Active document I can read the original block names of 5 instances, can anyone help me with this?


here is the code that I am using to load the blocks from the external dwg file to the current database 
 

private static void LoadBlocksFromExternalFile(Database currentDb, string filePath, Editor ed)

{

    try

    {

        using (Database externalDb = new Database(false, true))

        {

            externalDb.ReadDwgFile(filePath, FileShare.Read, true, "");

            ObjectIdCollection blockIds = new ObjectIdCollection();



            using (Transaction tr = externalDb.TransactionManager.StartTransaction())

            {

                BlockTable externalBt = (BlockTable)tr.GetObject(externalDb.BlockTableId, OpenMode.ForRead);



                foreach (ObjectId btrId in externalBt)

                {

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

                    if (!btr.IsLayout)

                    {

                        string blockName;



                        // Check if the block is anonymous

                        if (btr.IsAnonymous)

                        {

                            blockName = btr.Name; // Use the anonymous name directly

                            ed.WriteMessage($"\nAnonymous Block \"{blockName}\" loaded from file.");

                        }

                        else

                        {

                            // Create a temporary BlockReference to get the block name

                            BlockReference tempBlockRef = new BlockReference(Point3d.Origin, btrId);

                            blockName = tempBlockRef.IsDynamicBlock ?

                                ((BlockTableRecord)tempBlockRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name :

                                btr.Name;



                            if (tempBlockRef.IsDynamicBlock)

                            {

                                ed.WriteMessage($"\nBlock \"{blockName}\" is a dynamic block.");

                            }

                            ed.WriteMessage($"\nBlock \"{blockName}\" loaded from file.");

                        }



                        blockIds.Add(btrId);

                    }

                }



                tr.Commit();

            }



            if (blockIds.Count > 0)

            {

                IdMapping mapping = new IdMapping();

                currentDb.WblockCloneObjects(blockIds, currentDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);

            }

        }

    }

    catch (System.Exception ex)

    {

        Application.ShowAlertDialog($"Error loading blocks from file: {ex.Message}");

    }

}

 

Edited by SLW210
Added Code Tags!!
Posted

Please use Code Tags. (the <> in the editor toolbar)

 

I merged your threads, please keep related topics in 1 thread.

Posted

Was trying to use VLISP for this, but I also looking into VBA and C#.

 

You may need to go to a more C# specialized forum, though any .NET should be similar enough.

 

Did you even bother looking at the links I posted? Giles is still around on Autodesk Forum and maybe other sites AFAIK.

 

Really just starting with C# and AutoCAD, but from what I have researched you need something along these lines, more of a guideline than working code.

 

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

public class BlockVisibilityStates
{
    [CommandMethod("ReadVisibilityStates")]
    public void ReadVisibilityStates()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        
        // Start a transaction
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            // Open the BlockTable for read
            BlockTable blkTbl = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

            // Iterate through each block in the BlockTable
            foreach (ObjectId blkId in blkTbl)
            {
                BlockTableRecord blkRec = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);

                // Check if it's a dynamic block
                if (blkRec.IsDynamicBlock)
                {
                    // Iterate through the block references
                    foreach (ObjectId objId in blkRec)
                    {
                        if (objId.ObjectClass.Name == "AcDbBlockReference")
                        {
                            BlockReference blkRef = (BlockReference)tr.GetObject(objId, OpenMode.ForRead);

                            // Get the dynamic block reference property collection
                            DynamicBlockReferencePropertyCollection propCollection = blkRef.DynamicBlockReferencePropertyCollection;

                            foreach (DynamicBlockReferenceProperty prop in propCollection)
                            {
                                if (prop.PropertyName == "Visibility")
                                {
                                    // Read the visibility state
                                    var visibilityState = prop.Value.ToString();
                                    doc.Editor.WriteMessage($"Block: {blkRec.Name}, Visibility State: {visibilityState}\n");
                                }
                            }
                        }
                    }
                }
            }

            // Commit the transaction
            tr.Commit();
        }
    }
}

 

Untested

 

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...