.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bind Xrefs

13 REPLIES 13
Reply
Message 1 of 14
bertyboy
1632 Views, 13 Replies

Bind Xrefs

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
13 REPLIES 13
Message 2 of 14
bertyboy
in reply to: bertyboy

Hi Again! I just wanted to specifie that this code is for Autocad 2008.
And also Merry Christmas to all !!!
Message 3 of 14
Anonymous
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
Message 4 of 14
bertyboy
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
Message 5 of 14
coddy
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
Message 6 of 14
coddy
in reply to: bertyboy

And why the _?
Message 7 of 14
coddy
in reply to: bertyboy

Ok...I got it!

Dim BlkTabRec As BlockTableRecord = CType(bBTR, BlockTableRecord)

But unfortunatly it's not working.
Message 8 of 14
bertyboy
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
Message 9 of 14
coddy
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
Message 10 of 14
bertyboy
in reply to: bertyboy

You should be an expert of vb.net by the time! 🙂

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
Message 11 of 14
coddy
in reply to: bertyboy

BindXrefs function is fine and works great. It doesn't work if I run the scalelist command to reset the scales.

I haven't seen AutoCAD 2009. I'm really waiting to see.
Message 12 of 14
ismailcetin
in reply to: bertyboy

hi bertyboy,

Your Code is very Successful but i don't understand the row

bSave(bDwg)

Thanks
Message 13 of 14
matus.brlit
in reply to: bertyboy

This code was very helpful for me, thank you.
But sometimes, i get an error, when binding the xrefs. The error code is 304, Unknown error. I found an error code description, that says, "304 User is disabled", which gives me no clue.
But despite that error, the xrefs are bound. UNLESS they have nested xrefs, in this case, only the main xref is bound, not the nested ones.

Did anyone else come across this problem?
Message 14 of 14
AubelecBE
in reply to: bertyboy

hi.

have you try to modifiy your :

CommandMethod("BindAllXrefs")> _

by

CommandMethod("BindAllXrefs",CommandFlags.Session)> _

 

i have to use this flag.. I need to save and close the current document. but if i try this the BindXref not working....

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost