CAD2012插入文件作为块,块中的属性丢失。
插入代码如下: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 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
Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId,
OpenMode.ForWrite)
ModelSpace.AppendEntity(Block)
trans.AddNewlyCreatedDBObject(Block, True)
End If
trans.Commit()
End Using
End Using
End Sub
插入效果图,当前图像中没有ATT属性,但是块中有ATT属性。怎样才能在当前图形中也有ATT属性?
/// <summary>
/// 对块参考添加块属性
/// </summary>
/// <param name="ctrans">当前事务</param>
/// <param name="bRef">块参考</param>
/// <param name="btr">块参考所在的块记录</param>
/// <param name="recs"></param>
public static void SetAttributes(Transaction ctrans, BlockReference bRef, BlockTableRecord btr, sRecords recs)
{
try
{
//se.WriteMessage("\nsTr.AppendBlockAttibute strart " +bref.Handle);
if (bRef == null || btr == null) return;
if (!btr.HasAttributeDefinitions) return;
if (bRef.AttributeCollection != null && bRef.AttributeCollection.Count > 0)
{
if (recs != null)
{
foreach (ObjectId idAtt in bRef.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)ctrans.GetObject(idAtt, OpenMode.ForWrite);
if (recs != null)
{
attRef.TextString = recs.Data.ToString();
}
//se.WriteMessage("\ndbObj.GetType = {0}",dbObj.GetType().Name);
}
}
}
else
{
foreach (ObjectId idAtt in btr)
{
Entity ent = (Entity)ctrans.GetObject(idAtt, OpenMode.ForRead);
if (ent is AttributeDefinition)
{
AttributeDefinition attDef = (AttributeDefinition)ent;
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform);
if (attRef.Normal.Z == -1.0)
{
attRef.IsMirroredInX = true;
}
if (recs == null)
attRef.TextString = attRef.TextString;
else
{
if (recs != null)
{
attRef.TextString = recs.Data.ToString();
}
else
{
attRef.TextString = attRef.TextString;
}
}
bRef.AttributeCollection.AppendAttribute(attRef);
ctrans.AddNewlyCreatedDBObject(attRef, true);
}
}
}
//se.WriteMessage("\nsTr.AppendBlockAttibute End");
}
catch (System.Exception ex)
{
se.WriteMessage(ex);
}
} using (Transaction ctrans = sc.db.TransactionManager.StartTransaction())
{
BlockTable cbt = (BlockTable)ctrans.GetObject(sc.db.BlockTableId, OpenMode.ForRead);
BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(cbt, OpenMode.ForRead);
BlockReference bref = new BlockReference(point, cbt);
if (scale != null)
bref.ScaleFactors = scale;
bref.Rotation = rotation;
bref.XData = cbtr.XData;
reValue = sTr.AppendEntity(ctrans, bref, nSpace);
if (cbtr.HasAttributeDefinitions)
sTr.SetAttributes(ctrans, bref, cbtr, recs);
ctrans.Commit();
} /// <param name="recs">块参考的属性值集合</param> 但是在SetAttributes 函数的bRef.AttributeCollection.AppendAttribute(attRef);方法出错了,错误消息是eNoDatabase,不知道怎么回事。 你的代码? 插入的文件里有个属性是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
页:
[1]