乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 52|回复: 7

[编程交流] [vb.net] Modifying block attri

[复制链接]

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 11:39:24 | 显示全部楼层 |阅读模式
This is driving me crazy.
 
I'm am writing a program that inserts a drawing as a block and at the time of insertion changes a single attribute's text.
 
The code I'm using isn't working. When I set a breakpoint and inspect the blockreference that I am trying to retrieve info from, it seems to show that there is no attributereference in the block... which I don't believe is true because I know for a fact that the block contains an attribute. Obviouslt there's something I don't understand about retrieving and modifying block attributes with .net.
 
I know some of you use vb.net. Your help would be much appreciated. What am I doing wrong? What don't I understand about how to access block attribute info?
 
  1. [size=2][color=#0000ff][size=2][color=#0000ff]Sub[/color][/size][/color][/size][size=2] InsertBlock([/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] BlockPath [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]String[/color][/size][/color][/size][size=2], [/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] thePoint [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Point3d)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] doc [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] fname [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]String[/color][/size][/color][/size][size=2] = BlockPath[/size][size=2]HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] db [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Database = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] Database([/size][size=2][color=#0000ff][size=2][color=#0000ff]False[/color][/size][/color][/size][size=2], [/size][size=2][color=#0000ff][size=2][color=#0000ff]False[/color][/size][/color][/size][size=2])[/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2]db.ReadDwgFile(fname, System.IO.FileShare.Read, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2], [/size][size=2][color=#0000ff][size=2][color=#0000ff]Nothing[/color][/size][/color][/size][size=2])[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] t [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Transaction = doc.TransactionManager.StartTransaction[/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] idBTR [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] ObjectId = doc.Database.Insert(BlockPath, db, [/size][size=2][color=#0000ff][size=2][color=#0000ff]False[/color][/size][/color][/size][size=2])[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] bt [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] BlockTable = [/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] btr [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] BlockTableRecord = [/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] bref [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] BlockReference = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] BlockReference(thePoint, idBTR)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] theScale [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Scale3d = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] Scale3d(GetSystemVariable([/size][size=2][color=#a31515][size=2][color=#a31515]"DIMSCALE"[/color][/size][/color][/size][size=2]), _[/size][size=2]GetSystemVariable([/size][size=2][color=#a31515][size=2][color=#a31515]"DIMSCALE"[/color][/size][/color][/size][size=2]), _[/size][size=2]GetSystemVariable([/size][size=2][color=#a31515][size=2][color=#a31515]"DIMSCALE"[/color][/size][/color][/size][size=2]))[/size][size=2]bref.ScaleFactors = theScale[/size][size=2]bref.Layer = [/size][size=2][color=#a31515][size=2][color=#a31515]"HANGERS"[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] attIDs [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] AttributeCollection = bref.AttributeCollection[/size][size=2][color=#0000ff][size=2][color=#0000ff]For[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Each[/color][/size][/color][/size][size=2] attID [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] ObjectId [/size][size=2][color=#0000ff][size=2][color=#0000ff]In[/color][/size][/color][/size][size=2] attIDs[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] attRef [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] AttributeReference = t.GetObject(attID, OpenMode.ForWrite)[/size][size=2]attRef.TextString = [/size][size=2][color=#a31515][size=2][color=#a31515]"Test"[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Next[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2]btr.AppendEntity(bref)[/size][size=2]t.TransactionManager.AddNewlyCreatedDBObject(bref, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2])[/size][size=2][color=#0000ff][size=2][color=#0000ff]Finally[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](bref, IDisposable).Dispose()[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2]t.Commit()[/size][size=2][color=#0000ff][size=2][color=#0000ff]Finally[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](t, IDisposable).Dispose()[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Finally[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](db, IDisposable).Dispose()[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Sub[/color][/size][/color][/size]
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 11:55:18 | 显示全部楼层
Working with attributes does require a convoluted ass-backward flow.  See if this works out.
 
  1.     Sub InsertBlock(ByVal BlockPath As String, ByVal thePoint As Point3d)       Dim doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument       Dim fname As String = BlockPath       HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default)       Dim db As Database = New Database(False, False)       Try           db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)           Dim t As Transaction = doc.TransactionManager.StartTransaction           Try               Dim idBTR As ObjectId = doc.Database.Insert(BlockPath, db, False)               Dim bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)               Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)               Dim bref As BlockReference = New BlockReference(thePoint, idBTR)               Dim dblscale As Double = doc.Database.Dimscale               Dim theScale As Scale3d = New Scale3d(dblscale, dblscale, dblscale)               bref.ScaleFactors = theScale               bref.Layer = "HANGERS"               Try                   btr.AppendEntity(bref)                   t.TransactionManager.AddNewlyCreatedDBObject(bref, True)               Finally               End Try               Dim RefBTR As BlockTableRecord = t.GetObject(bref.BlockTableRecord, OpenMode.ForRead)               Dim attEnt As Entity               For Each attID As ObjectId In RefBTR                   attEnt = t.GetObject(attID, OpenMode.ForRead)                   If TypeOf attEnt Is AttributeDefinition Then                       Dim attDef As AttributeDefinition = attEnt                       Dim attRef As AttributeReference = New AttributeReference()                       attRef.SetAttributeFromBlock(attDef, bref.BlockTransform)                       Dim attRefID As ObjectId                       attRefID = bref.AttributeCollection.AppendAttribute(attRef)                       attRef.TextString = "Test"                       t.AddNewlyCreatedDBObject(attRef, True)                   End If               Next               t.Commit()               db.CloseInput(True)               db.Dispose()           Finally           End Try       Finally       End Try   End Sub
回复

使用道具 举报

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 11:59:49 | 显示全部楼层
That works. thanks man.
 
I'm a little hazy about what's going on in some of that code, particularly where the SetAttributeFromBlock method is invoked. Why does that method require a blocktransform? When I think of blocktransform I think of a transformation matrix... but that just doesn't seem right to me.
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 12:09:24 | 显示全部楼层
I can’t say I know the rational for that either.  Presumably, because it is an entity not yet fully defined (constant attributes notwithstanding) AutoCAD leaves some of the final details – transformations already established with the geometry for example – to the last possible moment.  
 
AutoCAD’s procrastinating, basically.   In that light, AutoCAD and I are part of the same fellowship.
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 12:19:36 | 显示全部楼层
Is it possible to have something that will help me (to insert a block  if possible as well) to update my block information in each drawing without opening each drawing
I have over 100 drawing and I have a block on each of them, and has few attributes in the block and I need to change one or all of them. All will be same throughout the project.

also i have a new block with attributes that i have to insert to all drwings.


Can you help me with this please?

Is it doable?
回复

使用道具 举报

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 12:29:47 | 显示全部楼层
 
What you're explaining is doable. You may not need vb.net to do it either. You may be able to accompish it with one of the more accessible languages such as lisp or VBA.
 
You may get more responces if you post your question as its own thread, or search for other people who have had similar issues in this forum.
 
- Joe
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 12:33:30 | 显示全部楼层
I have created a block in AutoCAD2009 with default layer and linetype, now i want to change the color of the block(want to edit the block) can any one help me in this regards
 
Thanks in advance!
 
Sandy
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 12:45:14 | 显示全部楼层
I assume you are interested in a .NET solution (based on the location of this new post):  Is that, indeed, the API of choice?
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 02:17 , Processed in 0.396754 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表