[求助]标注样式复制的问题
<p>我想在不打开图纸A的情况下把图纸A中的标注样式“a”复制到图纸B中,请问如何做?</p> 下面的代码以前贴过的,不过感觉太麻烦了,应该有简单的办法的:)public static void Test13()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database dbDesc = doc.Database;
Database dbSouce = new Database(false, true);
dbSouce.ReadDwgFile("e:\\dimstyles.dwg", FileShare.Read, true, null);
using (Transaction trSouce = dbSouce.TransactionManager.StartTransaction())
{
BlockTable btSouce = trSouce.GetObject(dbSouce.BlockTableId, OpenMode.ForRead) as BlockTable;
TextStyleTable tstSouce = trSouce.GetObject(dbSouce.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
LinetypeTable ltSouce = trSouce.GetObject(dbSouce.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
DimStyleTable dstSouce = trSouce.GetObject(dbSouce.DimStyleTableId, OpenMode.ForRead) as DimStyleTable;
DimStyleTableRecord dstrSouce = trSouce.GetObject(dstSouce["MyDimStyle"], OpenMode.ForRead) as DimStyleTableRecord;
using (Transaction trDesc = dbDesc.TransactionManager.StartTransaction())
{
BlockTable btDesc = trDesc.GetObject(dbDesc.BlockTableId, OpenMode.ForRead) as BlockTable;
TextStyleTable tstDesc = trDesc.GetObject(dbDesc.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
LinetypeTable ltDesc = trDesc.GetObject(dbDesc.LinetypeTableId, OpenMode.ForWrite) as LinetypeTable;
DimStyleTable dstDesc = trDesc.GetObject(dbDesc.DimStyleTableId, OpenMode.ForWrite) as DimStyleTable;
DimStyleTableRecord dstrDesc = new DimStyleTableRecord();
dstrDesc.CopyFrom(dstrSouce);
dstrDesc.Dimblk = GetBtrForm(dstrSouce.Dimblk, btSouce, btDesc);
dstrDesc.Dimblk1 = GetBtrForm(dstrSouce.Dimblk1, btSouce, btDesc);
dstrDesc.Dimblk2 = GetBtrForm(dstrSouce.Dimblk2, btSouce, btDesc);
dstrDesc.Dimldrblk = GetBtrForm(dstrSouce.Dimldrblk, btSouce, btDesc);
dstrDesc.Dimtxsty = GetTstrForm(trDesc, dstrSouce.Dimtxsty, tstSouce, tstDesc);
dstrDesc.Dimltex1 = GetLtrForm(trDesc, dstrSouce.Dimltex1, ltSouce, ltDesc);
dstrDesc.Dimltex2 = GetLtrForm(trDesc, dstrSouce.Dimltex2, ltSouce, ltDesc);
dstDesc.Add(dstrDesc);
trDesc.AddNewlyCreatedDBObject(dstrDesc, true);
trDesc.Commit();
}
trSouce.Commit();
}
}
//在数据库间复制文字样式
public static ObjectId GetTstrForm(Transaction tr, ObjectId rid, TextStyleTable tstSouce, TextStyleTable tstDesc)
{
if (rid != ObjectId.Null)
{
TextStyleTableRecord tstrSouce = (TextStyleTableRecord)rid.GetObject(OpenMode.ForRead);
ObjectId idRes = GetRecordId(tstDesc, tstrSouce.Name);
if (idRes == ObjectId.Null)
{
TextStyleTableRecord tstrDesc = new TextStyleTableRecord();
tstrDesc.CopyFrom(tstrSouce);
idRes = tstDesc.Add(tstrDesc);
tr.AddNewlyCreatedDBObject(tstrDesc, true);
}
return idRes;
}
return ObjectId.Null;
}
//在数据库间复制线型
public static ObjectId GetLtrForm(Transaction tr, ObjectId rid, LinetypeTable ltSouce, LinetypeTable ltDesc)
{
if (rid != ObjectId.Null)
{
LinetypeTableRecord ltrSouce = (LinetypeTableRecord)rid.GetObject(OpenMode.ForRead);
ObjectId idRes = GetRecordId(ltDesc, ltrSouce.Name);
if (idRes == ObjectId.Null)
{
LinetypeTableRecord ltrDesc = new LinetypeTableRecord();
ltrDesc.CopyFrom(ltrSouce);
idRes = ltDesc.Add(ltrDesc);
tr.AddNewlyCreatedDBObject(ltrDesc, true);
}
return idRes;
}
return ObjectId.Null;
}
//在数据库间复制块定义
public static ObjectId GetBtrForm(ObjectId rid, BlockTable btSouce, BlockTable btDesc)
{
if (rid != ObjectId.Null)
{
BlockTableRecord btrSouce = (BlockTableRecord)rid.GetObject(OpenMode.ForRead);
ObjectId idRes = GetRecordId(btDesc, btrSouce.Name);
if (idRes == ObjectId.Null)
{
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(rid);
btSouce.Database.Wblock(
btDesc.Database,
ids,
new Point3d(),
DuplicateRecordCloning.Replace
);
idRes = GetRecordId(btDesc, btrSouce.Name);
}
return idRes;
}
return ObjectId.Null;
}
//获取符号表记录名
public static ObjectId GetRecordId(SymbolTable st, string key)
{
if (st.Has(key))
{
ObjectId idres = st;
if (!idres.IsErased)
return idres;
foreach (ObjectId id in st)
{
if (!id.IsErased)
{
SymbolTableRecord str =
(SymbolTableRecord)id.GetObject(OpenMode.ForRead);
if (str.Name == key)
return id;
}
}
return st;
}
return ObjectId.Null;
}
<p>由于我最近一直忙,没有时间来论坛学习,今天进来一看,没有想到版主这么早就回复了,很是感动,只是版主,我用的vb.net,c#不会啊,您有vb.net的代码吗?</p> 我改成了vb.net的,但是还是有问题,请帮忙看看:
'VB.NET
Imports System.IO
'CAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class CopyCls
<CommandMethod("netcopy")> Public Shared Sub Test13()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim dbDesc As Database = doc.Database
Dim dbSouce As New Database(False, True)
dbSouce.ReadDwgFile("C:\1.dwg", FileShare.Read, True, Nothing)
Using trSouce As Transaction = dbSouce.TransactionManager.StartTransaction()
Dim btSouce As BlockTable = TryCast(trSouce.GetObject(dbSouce.BlockTableId, OpenMode.ForRead), BlockTable)
Dim tstSouce As TextStyleTable = TryCast(trSouce.GetObject(dbSouce.TextStyleTableId, OpenMode.ForRead), TextStyleTable)
Dim ltSouce As LinetypeTable = TryCast(trSouce.GetObject(dbSouce.LinetypeTableId, OpenMode.ForRead), LinetypeTable)
Dim dstSouce As DimStyleTable = TryCast(trSouce.GetObject(dbSouce.DimStyleTableId, OpenMode.ForRead), DimStyleTable)
Dim dstrSouce As DimStyleTableRecord = TryCast(trSouce.GetObject(dstSouce("1"), OpenMode.ForRead), DimStyleTableRecord)
Using trDesc As Transaction = dbDesc.TransactionManager.StartTransaction() '在这里就出错了
Dim btDesc As BlockTable = TryCast(trDesc.GetObject(dbDesc.BlockTableId, OpenMode.ForRead), BlockTable)
Dim tstDesc As TextStyleTable = TryCast(trDesc.GetObject(dbDesc.TextStyleTableId, OpenMode.ForWrite), TextStyleTable)
Dim ltDesc As LinetypeTable = TryCast(trDesc.GetObject(dbDesc.LinetypeTableId, OpenMode.ForWrite), LinetypeTable)
Dim dstDesc As DimStyleTable = TryCast(trDesc.GetObject(dbDesc.DimStyleTableId, OpenMode.ForWrite), DimStyleTable)
Dim dstrDesc As New DimStyleTableRecord()
dstrDesc.CopyFrom(dstrSouce)
dstrDesc.Dimblk = GetBtrForm(dstrSouce.Dimblk, btSouce, btDesc)
dstrDesc.Dimblk1 = GetBtrForm(dstrSouce.Dimblk1, btSouce, btDesc)
dstrDesc.Dimblk2 = GetBtrForm(dstrSouce.Dimblk2, btSouce, btDesc)
dstrDesc.Dimldrblk = GetBtrForm(dstrSouce.Dimldrblk, btSouce, btDesc)
dstrDesc.Dimtxsty = GetTstrForm(trDesc, dstrSouce.Dimtxsty, tstSouce, tstDesc)
dstrDesc.Dimltex1 = GetLtrForm(trDesc, dstrSouce.Dimltex1, ltSouce, ltDesc)
dstrDesc.Dimltex2 = GetLtrForm(trDesc, dstrSouce.Dimltex2, ltSouce, ltDesc)
dstDesc.Add(dstrDesc)
trDesc.AddNewlyCreatedDBObject(dstrDesc, True)
trDesc.Commit()
End Using
trSouce.Commit()
End Using
End Sub
Public Shared Function GetTstrForm(ByVal tr As Transaction, ByVal rid As ObjectId, ByVal tstSouce As TextStyleTable, ByVal tstDesc As TextStyleTable) As ObjectId
If rid <> ObjectId.Null Then
Dim tstrSouce As TextStyleTableRecord = DirectCast(rid.GetObject(OpenMode.ForRead), TextStyleTableRecord)
Dim idRes As ObjectId = GetRecordId(tstDesc, tstrSouce.Name)
If idRes = ObjectId.Null Then
Dim tstrDesc As New TextStyleTableRecord()
tstrDesc.CopyFrom(tstrSouce)
idRes = tstDesc.Add(tstrDesc)
tr.AddNewlyCreatedDBObject(tstrDesc, True)
End If
Return idRes
End If
Return ObjectId.Null
End Function
Public Shared Function GetLtrForm(ByVal tr As Transaction, ByVal rid As ObjectId, ByVal ltSouce As LinetypeTable, ByVal ltDesc As LinetypeTable) As ObjectId
If rid <> ObjectId.Null Then
Dim ltrSouce As LinetypeTableRecord = DirectCast(rid.GetObject(OpenMode.ForRead), LinetypeTableRecord)
Dim idRes As ObjectId = GetRecordId(ltDesc, ltrSouce.Name)
If idRes = ObjectId.Null Then
Dim ltrDesc As New LinetypeTableRecord()
ltrDesc.CopyFrom(ltrSouce)
idRes = ltDesc.Add(ltrDesc)
tr.AddNewlyCreatedDBObject(ltrDesc, True)
End If
Return idRes
End If
Return ObjectId.Null
End Function
Public Shared Function GetBtrForm(ByVal rid As ObjectId, ByVal btSouce As BlockTable, ByVal btDesc As BlockTable) As ObjectId
If rid <> ObjectId.Null Then
Dim btrSouce As BlockTableRecord = DirectCast(rid.GetObject(OpenMode.ForRead), BlockTableRecord)
Dim idRes As ObjectId = GetRecordId(btDesc, btrSouce.Name)
If idRes = ObjectId.Null Then
Dim ids As New ObjectIdCollection()
ids.Add(rid)
btSouce.Database.Wblock(btDesc.Database, ids, New Point3d(), DuplicateRecordCloning.Replace)
idRes = GetRecordId(btDesc, btrSouce.Name)
End If
Return idRes
End If
Return ObjectId.Null
End Function
Public Shared Function GetRecordId(ByVal st As SymbolTable, ByVal key As String) As ObjectId
If st.Has(key) Then
Dim idres As ObjectId = st(key)
If Not idres.IsErased Then
Return idres
End If
For Each id As ObjectId In st
If Not id.IsErased Then
Dim str As SymbolTableRecord = DirectCast(id.GetObject(OpenMode.ForRead), SymbolTableRecord)
If str.Name = key Then
Return id
End If
End If
Next
Return st(key)
End If
Return ObjectId.Null
End Function
End Class
似乎不要那么复杂,呵呵
看来以前是想多了:)
public static void Test13()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database dbDesc = doc.Database;
Database dbSouce = new Database(false, true);
dbSouce.ReadDwgFile("e:\\dimstyles.dwg", FileShare.Read, true, null);
using (Transaction trSouce = dbSouce.TransactionManager.StartTransaction())
{
DimStyleTable dstSouce = trSouce.GetObject(dbSouce.DimStyleTableId, OpenMode.ForRead) as DimStyleTable;
dbSouce.WblockCloneObjects
(
new ObjectIdCollection { dstSouce["MyDimStyle"] },
dbDesc.DimStyleTableId,
new IdMapping(),
DuplicateRecordCloning.UnmangleName,
false
);
}
}
<p>改成了vb.net的代码(还不完善,请大家自己改改),供需要的人学习,感谢飞狐大哥,感谢Fneix群友:</p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"><CommandMethod("t13")> Public Shared Sub Test13()<br/> Dim doc As Document = Application.DocumentManager.MdiActiveDocument<br/> Dim dbDesc As Database = doc.Database<br/> Dim dbSouce As New Database(False, True)<br/> dbSouce.ReadDwgFile("C:\1.dwg", FileShare.Read, True, Nothing)<br/> Using trSouce As Transaction = dbSouce.TransactionManager.StartTransaction()<br/> Dim dstSouce As DimStyleTable = TryCast(trSouce.GetObject(dbSouce.DimStyleTableId, OpenMode.ForRead), DimStyleTable)<br/> Dim newids As New ObjectIdCollection<br/> Dim iffind As Boolean = False<br/> For Each id As ObjectId In dstSouce<br/> Dim dstSouceRecord As DimStyleTableRecord = trSouce.GetObject(id, OpenMode.ForRead)<br/> If dstSouceRecord.Name = "100" Then<br/> iffind = True<br/> newids.Add(id)<br/> Exit For<br/> End If<br/> Next<br/> If iffind = True Then<br/> dbSouce.WblockCloneObjects(newids, dbDesc.DimStyleTableId, New IdMapping(), DuplicateRecordCloning.UnmangleName, False)<br/> Else<br/> MsgBox("没有100")<br/> End If<br/> End Using<br/> End Sub</font></p>
页:
[1]