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

Insert a Wblock

5 REPLIES 5
Reply
Message 1 of 6
cwhitaker
5019 Views, 5 Replies

Insert a Wblock

I have found a lot of exmples on inserting blocks already defined in the drawing but i am trying to figure out how to insert a w block?
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: cwhitaker

cwhitaker@powerflame.com schreef:
> I have found a lot of exmples on inserting blocks already defined in the
> drawing but i am trying to figure out how to insert a w block?

VB example:

Dim dbDwg As New Database
dbDwg.ReadDwgFile(blockFileName, IO.FileShare.Read, True, "")
blockdefObjId = workingDb.Insert(blockName, dbDwg, True)

--
http://cupocadnet.blogspot.com
Message 3 of 6
cwhitaker
in reply to: cwhitaker

Thanks for the help but i have one more question for you.

I am assuming that "workingDb" is the active document database but what is "blockdefObjId"?
Message 4 of 6
Anonymous
in reply to: cwhitaker


It's the ObjectID for the block reference in the
block table record.

 

Joe ...

 

 


style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">
Thanks
for the help but i have one more question for you. I am assuming that
"workingDb" is the active document database but what is
"blockdefObjId"?
Message 5 of 6
cwhitaker
in reply to: cwhitaker

Here is what i came up with in case anyone else search for this problem. The InsertBlock and SetAttributes code is from a book i bought called "VB.Net Programing for AutoCAD Customization" and i think it is a good book to learn with.

Public Shared Sub ImportBlock(ByVal BlockFile As String, ByVal BlockName As String)
Dim myDwg As ApplicationServices.Document
Dim myDwgLoc As DocumentLock
Dim mySourceDB As New Database
Dim myActiveDB As Database
'Read file and load block into current database
Try
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myActiveDB = myDwg.Database
mySourceDB.ReadDwgFile(BlockFile, IO.FileShare.Read, True, "")
myActiveDB.Insert(BlockName, mySourceDB, True)
mySourceDB.Dispose()
myDwgLoc.Dispose()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Sub

Public Shared Function InsertBlock(ByVal InsPt As Geometry.Point3d, ByVal BlockName As String, Optional ByVal XScale As Double = 1.0, Optional ByVal YScale As Double = 1.0, Optional ByVal ZScale As Double = 1.0) As DatabaseServices.ObjectId
Try
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As ApplicationServices.Document
Dim myBT As DatabaseServices.BlockTable
Dim myBTR As DatabaseServices.BlockTableRecord
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.Entity
Dim myBTREnum As BlockTableRecordEnumerator
Dim myDwgLoc As DocumentLock
'Get active document and begin transaction
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction
'Open Block Table
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
'Insert the Block
Dim myBlockDef As BlockTableRecord = myBT(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockRef As New DatabaseServices.BlockReference(InsPt, myBT(BlockName))
myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale)
myBTR.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
'Set Attribute values
myAttColl = myBlockRef.AttributeCollection
myBTREnum = myBlockDef.GetEnumerator
While myBTREnum.MoveNext
myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
End While
myDwgLoc.Dispose()
myTrans.Commit()
'Clean up
myTrans.Dispose()
myTransMan.Dispose()
Return myBlockRef.ObjectId
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Function

Public Shared Sub SetAttributes(ByVal BlockId As DatabaseServices.ObjectId, ByVal TagList As Collection)
Try
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As ApplicationServices.Document
Dim myDwgLoc As DocumentLock
Dim myBlkRef As DatabaseServices.BlockReference
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.ObjectId
Dim myAttRef As DatabaseServices.AttributeReference

'Get active document and begin transaction
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction
'Find block and set attributes
myBlkRef = BlockId.GetObject(OpenMode.ForWrite)
myAttColl = myBlkRef.AttributeCollection
For Each myEnt In myAttColl
myAttRef = myEnt.GetObject(OpenMode.ForWrite)
Dim myItem As String
Try
myItem = TagList(myAttRef.Tag)
myAttRef.TextString = myItem
Catch
End Try
Next
myDwgLoc.Dispose()
myTrans.Commit()
'Clean up
myTrans.Dispose()
myTransMan.Dispose()
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Sub

Here is how you call them:
Dim myBlk As DatabaseServices.ObjectId
myBlk = DwgTools.InsertBlock(New Geometry.Point3d(0, 0, 0), "titlebka")
Dim myTags As New Collection
myTags.Add("me", "Drawn_By")
myTags.Add("whatever", "Drawing_Number")
DwgTools.SetAttributes(myBlk, myTags)
DwgTools.ImportBlock("f:\autocad_dwgs\symbols\agen\supply1.dwg", "ChrisW")
DwgTools.InsertBlock(New Geometry.Point3d(5, 5, 0), "chrisw")
Message 6 of 6
Anonymous
in reply to: cwhitaker


Please either post in Rich Text format or attach a
text file.

 

Joe ...

 


style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">
Here
is what i came up with in case anyone else search for this problem. The
InsertBlock and SetAttributes code is from a book i bought called "VB.Net
Programing for AutoCAD Customization" and i think it is a good book to learn
with. Public Shared Sub ImportBlock(ByVal BlockFile As String, ByVal BlockName
As String) Dim myDwg As ApplicationServices.Document Dim myDwgLoc As
DocumentLock Dim mySourceDB As New Database Dim myActiveDB As Database 'Read
file and load block into current database Try myDwg =
ApplicationServices.Application.DocumentManager.MdiActiveDocument myDwgLoc =
myDwg.LockDocument() myActiveDB = myDwg.Database
mySourceDB.ReadDwgFile(BlockFile, IO.FileShare.Read, True, "")
myActiveDB.Insert(BlockName, mySourceDB, True) mySourceDB.Dispose()
myDwgLoc.Dispose() Catch ex As Exception MsgBox(ex.Message,
MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error") End Try End Sub
Public Shared Function InsertBlock(ByVal InsPt As Geometry.Point3d, ByVal
BlockName As String, Optional ByVal XScale As Double = 1.0, Optional ByVal
YScale As Double = 1.0, Optional ByVal ZScale As Double = 1.0) As
DatabaseServices.ObjectId Try Dim myTransMan As
DatabaseServices.TransactionManager Dim myTrans As
DatabaseServices.Transaction Dim myDwg As ApplicationServices.Document Dim
myBT As DatabaseServices.BlockTable Dim myBTR As
DatabaseServices.BlockTableRecord Dim myAttColl As
DatabaseServices.AttributeCollection Dim myEnt As DatabaseServices.Entity Dim
myBTREnum As BlockTableRecordEnumerator Dim myDwgLoc As DocumentLock 'Get
active document and begin transaction myDwg =
ApplicationServices.Application.DocumentManager.MdiActiveDocument myDwgLoc =
myDwg.LockDocument() myTransMan = myDwg.TransactionManager myTrans =
myTransMan.StartTransaction 'Open Block Table myBT =
myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead) myBTR =
myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
'Insert the Block Dim myBlockDef As BlockTableRecord =
myBT(BlockName).GetObject(OpenMode.ForRead) Dim myBlockRef As New
DatabaseServices.BlockReference(InsPt, myBT(BlockName))
myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale)
myBTR.AppendEntity(myBlockRef) myTrans.AddNewlyCreatedDBObject(myBlockRef,
True) 'Set Attribute values myAttColl = myBlockRef.AttributeCollection
myBTREnum = myBlockDef.GetEnumerator While myBTREnum.MoveNext myEnt =
myBTREnum.Current.GetObject(OpenMode.ForWrite) If TypeOf myEnt Is
DatabaseServices.AttributeDefinition Then Dim myAttDef As
DatabaseServices.AttributeDefinition = myEnt Dim myAttRef As New
DatabaseServices.AttributeReference myAttRef.SetAttributeFromBlock(myAttDef,
myBlockRef.BlockTransform) myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True) End If End While
myDwgLoc.Dispose() myTrans.Commit() 'Clean up myTrans.Dispose()
myTransMan.Dispose() Return myBlockRef.ObjectId Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try End Function Public Shared Sub SetAttributes(ByVal BlockId As
DatabaseServices.ObjectId, ByVal TagList As Collection) Try Dim myTransMan As
DatabaseServices.TransactionManager Dim myTrans As
DatabaseServices.Transaction Dim myDwg As ApplicationServices.Document Dim
myDwgLoc As DocumentLock Dim myBlkRef As DatabaseServices.BlockReference Dim
myAttColl As DatabaseServices.AttributeCollection Dim myEnt As
DatabaseServices.ObjectId Dim myAttRef As DatabaseServices.AttributeReference
'Get active document and begin transaction myDwg =
ApplicationServices.Application.DocumentManager.MdiActiveDocument myDwgLoc =
myDwg.LockDocument() myTransMan = myDwg.TransactionManager myTrans =
myTransMan.StartTransaction 'Find block and set attributes myBlkRef =
BlockId.GetObject(OpenMode.ForWrite) myAttColl = myBlkRef.AttributeCollection
For Each myEnt In myAttColl myAttRef = myEnt.GetObject(OpenMode.ForWrite) Dim
myItem As String Try myItem = TagList(myAttRef.Tag) myAttRef.TextString =
myItem Catch End Try Next myDwgLoc.Dispose() myTrans.Commit() 'Clean up
myTrans.Dispose() myTransMan.Dispose() Catch e As Exception MsgBox(e.Message,
MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error") End Try End Sub
Here is how you call them: Dim myBlk As DatabaseServices.ObjectId myBlk =
DwgTools.InsertBlock(New Geometry.Point3d(0, 0, 0), "titlebka") Dim myTags As
New Collection myTags.Add("me", "Drawn_By") myTags.Add("whatever",
"Drawing_Number") DwgTools.SetAttributes(myBlk, myTags)
DwgTools.ImportBlock("f:\autocad_dwgs\symbols\agen\supply1.dwg", "ChrisW")
DwgTools.InsertBlock(New Geometry.Point3d(5, 5, 0),
"chrisw")

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