batch xref bind vb.net

batch xref bind vb.net

yoitsarun
Advocate Advocate
2,420 Views
15 Replies
Message 1 of 16

batch xref bind vb.net

yoitsarun
Advocate
Advocate

i tried to batch bind xrefs but getting "invalid input error"

could any one please help me please:

Namespace FormVB
    Public Class RunApp
        <autodesk.autocad.runtime.commandmethod("bxb", autodesk.autocad.runtime.commandflags.session)="">
        Public Shared Sub InitForm()
            Dim frm As New BatchXrefBind
            Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
              Dim result As DialogResult = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm)

        End Sub
    End Class
End Namespace


Public Class BatchXrefBind
    Private Sub button_Publish_Click(sender As Object, e As EventArgs) Handles button_Publish.Click

        
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
                Dim openFile1 As OpenFileDialog = New OpenFileDialog()

               openFile1.InitialDirectory = "C\"
        openFile1.Filter = "Drawing Files|*.dwg"
        openFile1.Multiselect = True

                If openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If openFile1.FileNames.Length > 0 Then
                Dim strFile As String
                For Each strFile In openFile1.FileNames

 Using db As Database = New Database(False, True)
                        db.ReadDwgFile(strFile, FileOpenMode.OpenForReadAndAllShare, False, Nothing)

 Dim bTransMan As DatabaseServices.TransactionManager
                        Dim bTrans As DatabaseServices.Transaction
                        Dim bDwg As Document
                        Dim bBT As BlockTable
                        Dim bBTR As SymbolTableRecord
                        Dim bBTE As SymbolTableEnumerator
                        Dim bBTRr As BlockTableRecord

                       
                        bDwg = Application.DocumentManager.MdiActiveDocument
                        bTransMan = bDwg.TransactionManager

                        bTrans = bTransMan.StartTransaction
                        Try
                            
                            bBT = bDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
                            bBTE = bBT.GetEnumerator

                            Dim XrefIds As New ObjectIdCollection

                            While bBTE.MoveNext = True
                                bBTR = bBTE.Current.GetObject(OpenMode.ForRead)

                                bBTRr = CType(bBTR, BlockTableRecord)

                                
                                If bBTRr.IsFromExternalReference And bBTRr.IsResolved Then
                                    XrefIds.Add(bBTR.Id)
                                End If

                            End While

                            
                            Try
                                bDwg.Database.BindXrefs(XrefIds, False)
                            Catch ex As Exception
                                MsgBox("Error: " & ex.Message)
                            End Try

                             bTrans.Commit()
                            

                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Critical)
                        Finally
                            bTrans.Dispose()
                            bTransMan.Dispose()
                        End Try


                        db.SaveAs(db.OriginalFileName, True, db.OriginalFileVersion, db.SecurityParameters)

                    End Using
                Next
            End If
        End If

 End Sub


</autodesk.autocad.runtime.commandmethod("bxb",>
0 Likes
Accepted solutions (1)
2,421 Views
15 Replies
Replies (15)
Message 2 of 16

_gile
Consultant
Consultant

Hi,

 

I'm not very cumfortable with VB but this line hurts me:

openFile1.InitialDirectory = "C\"

If this is not the source of the error, could you say which line throws the "invalid input error" error.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 16

yoitsarun
Advocate
Advocate

error message is from here:

 

Try
bDwg.Database.BindXrefs(XrefIds, False) Catch ex As Exception MsgBox("Error: " & ex.Message) End Try

 

0 Likes
Message 4 of 16

yoitsarun
Advocate
Advocate

one more thing this code is working if i run individually (not as batch).

0 Likes
Message 5 of 16

_gile
Consultant
Consultant

To know the line the error occured, you can try replacing the Catch block with this:

Try
bDwg.Database.BindXrefs(XrefIds, False) Catch ex As Exception MsgBox("Error: " & ex.Message & vbCr & ex.StackTrace) End Try

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 16

yoitsarun
Advocate
Advocate

error is:

 Capture.PNG

0 Likes
Message 7 of 16

_gile
Consultant
Consultant

The error is due to line 183 in the file BatchXrefBind.vb.

 

error.png



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 16

yoitsarun
Advocate
Advocate

yes, but i dont know what is the error in this code

bDwg.Database.BindXrefs(XrefIds, False)

 

0 Likes
Message 9 of 16

_gile
Consultant
Consultant

The error is probably due the the contents of XrefIds.

 

Pay attention to what's said i the documentation for the BindXrefs method:

"The object IDs in xrefIds must all be from the database and they must all be resolved."

 

It seems to me bDwg is the active document (i.e. the document where the command was launched) and XrefIds contains objectIds from the opened in memory database.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 16

yoitsarun
Advocate
Advocate

something wrong in active document ?

I am new to this, but I did xref bind program in editor using same code and it works.

How it will work in memory database, if you know something please help me.

If you are in familiar in c# it is ok. I can convert.

0 Likes
Message 11 of 16

_gile
Consultant
Consultant

"The object IDs in xrefIds must all be from the database" i.e., the database you create 'in memory' (db). 

Try:

db.BindXrefs(XrefIds, False)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 16

yoitsarun
Advocate
Advocate

I tried this, but no luck, same "error: invalid input"

0 Likes
Message 13 of 16

_gile
Consultant
Consultant

Try like this (not tested)

 

using System.Windows.Forms;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

using AcCoreAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace BindXrefsSample
{
    public class Commands
    {
        [CommandMethod("TEST1")]
        public static void BindXrefsInCurrentDrawing()
        {
            BindXrefs(HostApplicationServices.WorkingDatabase);
        }

        [CommandMethod("TEST2")]
        public static void BindXrefsInDwgFiles()
        {
            var ofd = new OpenFileDialog{
                InitialDirectory = "C:\\",
                Filter = "Drawing files|*.dwg",
                Multiselect = true};
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (string fileName in ofd.FileNames)
                {
                    using (var db = new Database(false, true))
                    {
                        db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                        BindXrefs(db);
                        db.SaveAs(fileName, true, db.OriginalFileVersion, db.SecurityParameters);
                    }
                }
            }
        }

        private static void BindXrefs(Database db)
        {
            try
            {
                var xrefIds = new ObjectIdCollection();
                using (var tr = new OpenCloseTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    foreach (ObjectId id in bt)
                    {
                        var btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                        if (btr.IsFromExternalReference && btr.IsResolved)
                            xrefIds.Add(id);
                    }
                }
                db.BindXrefs(xrefIds, false);
            }
            catch (System.Exception ex)
            {
                AcCoreAp.ShowAlertDialog("Error: " + ex.Message);
            }
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 16

_gile
Consultant
Consultant
Accepted solution

This version of the BindXrefs() method resolves the xrefs in the database before binding them.

 

        private static void BindXrefs(Database db)
        {
            var workingDb = HostApplicationServices.WorkingDatabase;
            HostApplicationServices.WorkingDatabase = db;
            try
            {
                db.ResolveXrefs(true, false);
                var xrefIds = new ObjectIdCollection();
                using (var tr = new OpenCloseTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    foreach (ObjectId id in bt)
                    {
                        var btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                        if (btr.IsFromExternalReference && btr.IsResolved)
                            xrefIds.Add(id);
                    }
                }
                db.BindXrefs(xrefIds, false);
            }
            catch (System.Exception ex)
            {
                AcCoreAp.ShowAlertDialog("Error: " + ex.Message);
            }
            finally
            {
                HostApplicationServices.WorkingDatabase = workingDb;
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 15 of 16

yoitsarun
Advocate
Advocate

thanks a lot,

I will try this in the evening , and will update the results. thanks _gile 

 

 

0 Likes
Message 16 of 16

yoitsarun
Advocate
Advocate

and it is working thanks gile

0 Likes