• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 18
    Registered: ‎09-25-2007

    Bind Xrefs

    705 Views, 13 Replies
    12-24-2007 01:51 PM
    Hi to all. I have'nt seen a lot on xrefs bind. So I've done an effort on my side and wanted to share this code with you. It's a basic Bind Command. You can send me your comments if you wish!
    Don't forget to put the < before the commandmethod!!!

    Imports Autodesk.AutoCAD
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.Runtime

    CommandMethod("BindAllXrefs")> _
    Public Sub BES()
    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

    bDwg = Application.DocumentManager.MdiActiveDocument
    bTransMan = bDwg.TransactionManager
    bTrans = bTransMan.StartTransaction
    Try
    'Open the database for Read
    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)

    'Check if block is xref and add if so, add it's ID to the ObjectIdCollection
    If bBTR.IsResolved And Not bBTR.Name.Contains("|") Then
    XrefIds.Add(bBTR.Id)
    End If

    End While

    'Bind xref of collection
    bDwg.Database.BindXrefs(XrefIds, True)

    'Important to commit
    bTrans.Commit()

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

    End Sub
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎09-25-2007

    Re: Bind Xrefs

    12-24-2007 09:30 PM in reply to: bertyboy
    Hi Again! I just wanted to specifie that this code is for Autocad 2008.
    And also Merry Christmas to all !!!
    Please use plain text.
    Valued Contributor
    Posts: 50
    Registered: ‎07-12-2004

    Re: Bind Xrefs

    01-28-2008 10:17 PM in reply to: bertyboy
    Hi,

    Thanks for the code. But I have a question. What if the xrefs are unresolved? How do I handle the unresolved xrefs, like 'Not Found', 'Unloaded', 'Unreferenced'?

    Thanks
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎09-25-2007

    Re: Bind Xrefs

    01-29-2008 03:41 PM in reply to: bertyboy
    I love good questions!
    I'm not an expert on this. But I find out that you could convert the
    "SymbolTableRecord" to a "BlockTableRecord" with:

    dim BlcTabRec as BlockTableRecord = _ Ctype(YourSymTableRecVariable,BlockTableRecord)

    the BlcTabRec have a couple of property you could use.
    Like:
    IsUnloaded
    IsFormExternalReference
    IsResolved

    But for the:
    'Not Found', 'Unreferenced'
    I think you could use the "IsResolved" property.

    If Not IsResolved(BlcTabRec ) then Msgbox "Problem here!!"

    Just for information, there's an automatic Bind command part of the etransmit command in Autocad 2008. That's very cool!

    Cad Programmer
    Bert
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎02-17-2008

    Re: Bind Xrefs

    02-17-2008 10:39 PM in reply to: bertyboy
    You're great, man!

    dim BlcTabRec as BlockTableRecord = _ Ctype(YourSymTableRecVariable,BlockTableRecord)

    What is YourSymTableRecVariable? Could you please help on how that would go into code.

    Thanks
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎02-17-2008

    Re: Bind Xrefs

    02-17-2008 10:39 PM in reply to: bertyboy
    And why the _?
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎02-17-2008

    Re: Bind Xrefs

    02-17-2008 11:19 PM in reply to: bertyboy
    Ok...I got it!

    Dim BlkTabRec As BlockTableRecord = CType(bBTR, BlockTableRecord)

    But unfortunatly it's not working.
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎09-25-2007

    Re: Bind Xrefs

    02-18-2008 05:55 PM in reply to: bertyboy
    Me it's working very fine. So I send you the code updated.
    Oh ya! You guessed right for what you wrote!

    Imports Autodesk.AutoCAD
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Runtime
    Imports Microsoft.Office.Interop

    Imports Microsoft.VisualBasic.ApplicationServices
    Imports System.IO

    < CommandMethod("BindAllXrefs") > _
    Public Sub BES()
    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

    ' Current Document
    bDwg = Application.DocumentManager.MdiActiveDocument
    bTransMan = bDwg.TransactionManager

    bTrans = bTransMan.StartTransaction
    Try
    ' Open the database for Read
    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)

    ' Check if block is xref and also resolved and thenadd its ID to the ObjectIdCollection
    If bBTRr.IsFromExternalReference And bBTRr.IsResolved Then
    XrefIds.Add(bBTR.Id)
    End If

    End While

    ' Bind xref of collection
    Try
    'We bind all xrefs of drawing
    bDwg.Database.BindXrefs(XrefIds, True)
    Catch ex As Exception
    MsgBox("Error: " & ex.Message)
    End Try

    ' Important to commit
    bTrans.Commit()
    bSave(bDwg)

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

    End Sub

    Maybe if it doesn't work, a error message could help you up!

    Cad Programmer
    Bert
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎02-17-2008

    Re: Bind Xrefs

    04-06-2008 05:21 AM in reply to: bertyboy
    After a long gap!

    Thanks for the code. I did some modifications in the code to suit my requirements, and it did work fine. But after I run a certain AutoCAD command, I get an error that says 'eWasOpenForWrite'. Seems like the command is opening the database and forgets to close! Is there any workaround that can be done in our code?

    Thanks
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎09-25-2007

    Re: Bind Xrefs

    04-07-2008 03:09 PM in reply to: bertyboy
    You should be an expert of vb.net by the time! :smileyhappy:

    I don't know, I haven't gone more far on this issue. And it seems that we are two caring about that! Everything seems to be ok but maybe there a problem with the Autodesk BindXrefs function. Maybe they'll enhance it for Autocad 2009. Have you seen it! The interface is pretty cool, I should say awesome!

    Bert
    Cad programmer
    Please use plain text.