将DWG文件作为块插入当前图形的VB代码
请问哪位老大知道?还烦请相告。 <P>明经的.NET开发版块做的不好。没有人气。越来越……</P> <P>Public Function InsertBlock(ByVal sourceFileName As String, ByVal newBlockName As String, ByVal po As Point3d) As ObjectId<BR> 'Dim sourceFileName As String = "E:\FreeNEST2\FreeNEST2\bin\Project\My test project\Part\Part1.dwg"<BR> 'Dim newBlockName As String = "Part1"<BR> Dim db As Database = HostApplicationServices.WorkingDatabase()<BR> Dim trans As Transaction = db.TransactionManager.StartTransaction()<BR> Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)<BR> Dim btr As BlockTableRecord = trans.GetObject(bt(btr.ModelSpace), OpenMode.ForWrite)<BR> Try<BR> Dim sourceDatabase As Database = GetDatabaseFromFile(sourceFileName)<BR> '把源数据库模型空间中的实体插入到当前数据库的一个新的块表记录中<BR> Dim bobj As ObjectId = HostApplicationServices.WorkingDatabase.Insert(newBlockName, sourceDatabase, False)<BR> Dim bref As BlockReference = New BlockReference(po, bobj)<BR> Dim blockobj As ObjectId = btr.AppendEntity(bref)<BR> ''''<BR> Dim empBtr As BlockTableRecord = trans.GetObject(bt(newBlockName), OpenMode.ForRead)<BR> Dim id As ObjectId<BR> For Each id In empBtr<BR> Dim ent As Entity = trans.GetObject(id, OpenMode.ForRead, False)<BR> If TypeOf ent Is AttributeDefinition Then<BR> Dim attRef As AttributeReference = New AttributeReference<BR> Dim attDef As AttributeDefinition = CType(ent, AttributeDefinition)<BR> attRef.SetPropertiesFrom(attDef)<BR> attRef.Position = New Point3d(bref.Position.X + attDef.Position.X, bref.Position.Y + attDef.Position.Y, bref.Position.Z + attDef.Position.Z)<BR> attRef.Height = attDef.Height<BR> attRef.Rotation = attDef.Rotation<BR> attRef.Tag = attDef.Tag<BR> attRef.TextString = attDef.TextString<BR> bref.AttributeCollection.AppendAttribute(attRef)<BR> trans.AddNewlyCreatedDBObject(attRef, True)<BR> End If<BR> Next<BR> '''<BR> trans.AddNewlyCreatedDBObject(bref, True)<BR> Return blockobj<BR> Catch e As System.Exception<BR> Application.ShowAlertDialog(e.Message)<BR> End Try<BR> '''<BR> End Function</P><P> Private Function GetDatabaseFromFile(ByVal fileName As String) As Database<BR> '''<BR> Dim databaseFromFile As Database = New Database(False, True)<BR> databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.Read, False, DBNull.Value.ToString)<BR> '为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把源数据库对象关闭。<BR> databaseFromFile.CloseInput(True)<BR> Return databaseFromFile<BR> End Function</P>
<P> </P>
<P>源程序,有才鸟老大的也有我自已的东西。希望对大家有用。</P> <p>不行 ,复制过去不能用 </p><p>另外再请教 ,我会VBA,要再学VB.net 二次开发需从头开始吗?</p> 会VBA会有些帮助,但不是完全相同。 <p>关闭时出现致命错误,为什么?QQ420021327</p> <p>我是在VB中用的</p><p>先将需要插入的dwg当作外部参照插入,然后再绑定为块</p><p>可能这不是最好的办法,但是当时没办法,想了好久才解决的一个办法。</p><p></p><p>''''''''插入图</p><p> Dim insertedBlock As Object 'AcadExternalReference<br/> <br/> Pt_Temp_1(0) = 0<br/> Pt_Temp_1(1) = 0<br/> Pt_Temp_1(2) = 0<br/> <br/> <br/> TXT_STR = App.Path & "\twz.dwg"<br/> Set insertedBlock = AcadDoc.ModelSpace.AttachExternalReference(TXT_STR, "TWZ", Pt_Temp_1, 1, 1, 1, 0, False)<br/> <br/> AcadDoc.Blocks.Item(insertedBlock.Name).Bind False<br/> <br/> <br/> </p>
页:
[1]