- 积分
- 549
- 明经币
- 个
- 注册时间
- 2014-5-23
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2014-10-20 16:13:30
|
显示全部楼层
插入的文件里有个属性是ATT_ABC。我初学CAD开发,很多东西模模糊糊的,请大侠帮忙看看。谢谢!!
Private Sub InsertBlock(ByVal point As Point3d, ByVal blockFile As String, ByVal blockName As String, ByVal rotation As Double)
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acDatabase As Database = acDoc.Database
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor()
Dim fileData As FileInfo = My.Computer.FileSystem.GetFileInfo(blockFile)
Using (acDoc.LockDocument)
Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
Dim acBlockTable As BlockTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForWrite)
Dim FilePath As String = blockFile
Dim FileName As String = fileData.Name
Dim db As New Database(False, False)
db.ReadDwgFile(FilePath, IO.FileShare.Read, True, "")
Dim id As ObjectId = acDatabase.Insert(FileName, db, True)
If id.IsValid = False Then
Exit Sub
End If
If (acBlockTable.Has(blockName)) Then
Dim Block As BlockReference = New BlockReference(point, acBlockTable(blockName))
Block.ScaleFactors = New Scale3d(1, 1, 1)
Block.Rotation = rotation
Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
ModelSpace.AppendEntity(Block)
trans.AddNewlyCreatedDBObject(Block, True)
Else
Dim tr As BlockTableRecord = id.GetObject(OpenMode.ForWrite)
tr.Name = blockName
Dim Block As BlockReference = New BlockReference(point, id)
Block.ScaleFactors = New Scale3d(1, 1, 1)
Block.Rotation = rotation
If tr.HasAttributeDefinitions Then
Dim srlt As New SortedList()
srlt.Add("ATT_ABC", "sss")
SetAttributes(trans, Block, tr, srlt)
End If
Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
ModelSpace.AppendEntity(Block)
trans.AddNewlyCreatedDBObject(Block, True)
End If
Public Shared Sub SetAttributes(ByVal ctrans As Transaction, ByVal bRef As BlockReference, ByVal btr As BlockTableRecord, ByVal recs As SortedList)
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor()
Try
'se.WriteMessage("\nsTr.AppendBlockAttibute strart " +bref.Handle);
If bRef Is Nothing OrElse btr Is Nothing Then
Return
End If
If Not btr.HasAttributeDefinitions Then
Return
End If
If bRef.AttributeCollection IsNot Nothing AndAlso bRef.AttributeCollection.Count > 0 Then
If recs IsNot Nothing Then
For Each idAtt As ObjectId In bRef.AttributeCollection
Dim attRef As AttributeReference = DirectCast(ctrans.GetObject(idAtt, OpenMode.ForWrite), AttributeReference)
If recs(attRef.Tag) IsNot Nothing Then
attRef.TextString = recs(attRef.Tag).Data.ToString()
'se.WriteMessage("\ndbObj.GetType = {0}",dbObj.GetType().Name);
End If
Next
End If
Else
For Each idAtt As ObjectId In btr
Dim ent As Entity = DirectCast(ctrans.GetObject(idAtt, OpenMode.ForRead), Entity)
If TypeOf ent Is AttributeDefinition Then
Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
Dim attRef As New AttributeReference()
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform)
If attRef.Normal.Z = -1.0 Then
attRef.IsMirroredInX = True
End If
If recs Is Nothing Then
attRef.TextString = attRef.TextString
Else
If recs(attRef.Tag) IsNot Nothing Then
attRef.TextString = recs(attRef.Tag).ToString()
Else
attRef.TextString = attRef.TextString
End If
ed.WriteMessage(vbCrLf & " ==> ATT =>" & attRef.Tag & " = " & attRef.TextString)
End If
bRef.AttributeCollection.AppendAttribute(attRef)
ctrans.AddNewlyCreatedDBObject(attRef, True)
End If
Next
'se.WriteMessage("\nsTr.AppendBlockAttibute End");
End If
Catch ex As System.Exception
ed.WriteMessage(vbCrLf & "ERR ==> " & ex.Message)
End Try
End Sub
trans.Commit()
End Using
End Using
End Sub |
|