Wblock Objects out of drawing in to there own drawings

Wblock Objects out of drawing in to there own drawings

Anonymous
Not applicable
1,386 Views
7 Replies
Message 1 of 8

Wblock Objects out of drawing in to there own drawings

Anonymous
Not applicable

Would somone eb able to help me 

 

I am wanting to wblock out each block as i write to my text file but i cant seem to find the correct command to do it 

 

this is my code so far 

 

Try
                        ' Build a filter list so that only
                        ' block references are selected
                        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
                        Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "INSERT")}
                        Dim filter As New SelectionFilter(filList)
                        Dim res As PromptSelectionResult = ed.SelectAll(filter)


                        ' Do nothing if selection is unsuccessful
                        If res.Status <> PromptStatus.OK Then
                            Return
                        End If
                        Dim selSet As SelectionSet = res.Value
                        Dim idArray As ObjectId() = selSet.GetObjectIds()

                        logfile.Write("Block Insersion" & vbTab & "Layer" & vbTab & "Block" & vbTab & "GeometricExtents" & vbTab & "Min" & vbTab & "Max" & vbTab & "Correct Insersion" & vbTab & "L" & vbTab & "D" & vbTab & "H")
                        Dim objIds As New ObjectIdCollection()
                        For Each id As ObjectId In idArray

                            'Dim ent As Entity = TryCast(tr.GetObject(objId, OpenMode.ForRead), Entity)
                            'Dim br As BlockReference = TryCast(ent, BlockReference)

                            Dim blkRef As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
                            Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite), BlockTableRecord)



                            If Not str.Contains(btr.Name.ToUpper) Then




                                logfile.Write(Environment.NewLine)
                                logfile.Write(blkRef.Position.ToString & vbTab)

                                logfile.Write(blkRef.Layer & vbTab)
                                logfile.Write(btr.Name & vbTab)

                                If blkRef.Bounds.HasValue Then
                                    Dim exts As Extents3d = blkRef.GeometricExtents
                                    Dim pt1 = exts.MinPoint
                                    Dim pt2 = exts.MaxPoint
                                    logfile.Write(blkRef.GeometricExtents.ToString & vbTab)
                                    logfile.Write(pt1.ToString & vbTab)
                                    logfile.Write(pt2.ToString & vbTab)
                                    logfile.Write(pt1.X.ToString & "," & pt2.Y.ToString & vbTab)
                                    logfile.Write(pt2.X - pt1.X & vbTab)
                                    logfile.Write(pt2.Y - pt1.Y & vbTab)
                                    logfile.Write(pt2.Z - pt1.Z & vbTab)

                                    If CheckBox1.Checked Then
                                        blkRef.Position = New Point3d(Convert.ToDouble(pt1.X.ToString), Convert.ToDouble(pt2.Y.ToString), Convert.ToDouble(0))
                                        logfile.Write(blkRef.Position.ToString & vbTab)
                                        objIds.Clear()
                                        objIds.Add(id)


THIS IS WHERE I WOULD LIKE TO WBLOCK



                                        objIds.Clear()



                                    End If
                                Else
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                    logfile.Write("No Extents" & vbTab)
                                End If


                                str.Add(btr.Name.ToUpper)

                            End If






                            btr.Dispose()



                        Next



                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        MsgBox(("Exception: " + ex.Message))
                        logfile.Write(Environment.NewLine)
                    Finally

                        tr.Dispose()

                    End Try

I was trying to do it with the objIds but i have had no luck 

0 Likes
1,387 Views
7 Replies
Replies (7)
Message 2 of 8

SENL1362
Advisor
Advisor

This Pseudo code might help:

 

BlockReference blkRef;
...
var newBlkDb=db.Wblock(blkRef.BlockTableRecord);
var blkPathname=Path.Combine(@"c:\temp", blkRef.Name +".dwg");
if (File.Exists(blkPathname))
File.Delete(blkPathname);
newBlkDb.SaveAs(blkPathname, DwgVersion.Current);

 

 

0 Likes
Message 3 of 8

BKSpurgeon
Collaborator
Collaborator

The relevant entry in the autocad documentation (I recommend just cutting and pasting it) and applying it accordingly to your code - if you have further issues then please repost.

 

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS73099cc1...

 

 

0 Likes
Message 4 of 8

Anonymous
Not applicable

Just tried putting 

 

Dim newBlkDb = db.Wblock(blkRef.BlockTableRecord)
                                        Dim blkPathname = Path.Combine("c:\temp", blkRef.Name + ".dwg")
                                        If File.Exists(blkPathname) Then
                                            File.Delete(blkPathname)
                                        End If
                                        newBlkDb.SaveAs(blkPathname, DwgVersion.Current)

and get 

 

'db.AcadDatabase' threw an exception of type 'System.AccessViolationException'
'db.ApproxNumObjects' threw an exception of type 'System.AccessViolationException'

0 Likes
Message 5 of 8

Anonymous
Not applicable

Im not sure if the link that you have posted is correct i cant find any relivant code 

0 Likes
Message 6 of 8

SENL1362
Advisor
Advisor

is the AccessViolation coming up while creating the new Database?

0 Likes
Message 7 of 8

Anonymous
Not applicable

This is the line i am getting that message on 

 

Dim newBlkDb = db.Wblock(blkRef.BlockTableRecord)

If i hover over the DB.Wblock and expand it that is the error that i get 

0 Likes
Message 8 of 8

SENL1362
Advisor
Advisor


Do you get this error when you WBLOCK you're block from the WBLOCK command itself?

This sample does work for AutoCAD 2016.
It starts with the BlockName -- i didn't add you're logic to select the block from the block reference.
One thing to keep in mind is that you obviously  cannot export the block to a filename equal to the (main) drawing.
Try to narrow down the problem to the block export and then when solved add youre logic again.

 

 

       [CommandMethod("ExpBlk")]
        public void ExportBlock()
        {
            try
            {
 
                Document doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc == null)
                    throw new System.Exception("No MdiActiveDocument");
                Database db = doc.Database;

                string blkName = "MyBlock";
                var blkId = GetBlockId(db, blkName);
                if (blkId.IsNull)
                    throw new System.Exception("Block not found: " + blkName);

                string blkPath=@"c:\Temp";
                string blkPathname=Path.Combine(blkPath,blkName + ".dwg");
                var exportStatus=ExportBlock(db, blkName, blkPathname);
                if (!exportStatus)
                    throw new System.Exception("ExportBlock failed for: " + blkName);
            }
            catch //(System.Exception ex)
            {
                
            }
        }



        public bool ExportBlock(Database db, string blkName, string blkPathname)
        {
            bool blkExported = false;

            if (db == null)
                return false;

            if (string.IsNullOrWhiteSpace(blkName))
                return false;

            if (string.IsNullOrWhiteSpace(blkPathname))
            {
                var dbPath=Path.GetDirectoryName(db.Filename);
                if (String.IsNullOrWhiteSpace(dbPath))
                    dbPath=@"c:\Temp";   //or solve the Env.Var %TEMP%
                blkPathname=Path.Combine(dbPath, blkName+".dwg");
            }

	    using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                ObjectId blkId=ObjectId.Null;
                if (bt.Has(blkName))
                    blkId=bt[blkName];
                if (!blkId.IsNull)
                {
                   BlockTableRecord blk=(BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
                   var blkDb=db.Wblock(blkId);
                    if (blkDb!=null)
                    {
                        if (File.Exists(blkPathname))
                            File.Delete(blkPathname);
                        blkDb.SaveAs(blkPathname,DwgVersion.Current);
                        if (File.Exists(blkPathname))
                            blkExported=true;
                    }
                }
                tr.Commit();
            }

            return blkExported;
        }


        public ObjectId GetBlockId(Database db, string blkName)
        {
            ObjectId blkId = ObjectId.Null;

            if (db == null)
                return ObjectId.Null;

            if (string.IsNullOrWhiteSpace(blkName))
                return ObjectId.Null;

            using (Transaction tr=db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blkName))
                    blkId = bt[blkName];
                
                tr.Commit();
            }
            return blkId;
        }


0 Likes