乐筑天下

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

将文本转换为属性

[复制链接]

3

主题

28

帖子

1

银币

初来乍到

Rank: 1

铜币
40
发表于 2010-7-30 06:44:13 | 显示全部楼层 |阅读模式
朋友们好
我的目标是将文字作为属性添加到块参照中。下面的代码似乎有效
当我面临严峻挑战时,它又出现在F#中。抱歉
我想知道是否有比<br>1)通过过滤选择集选择对象和<br>2)避免新属性中所有相关文本属性的繁琐初始化更好的方法(我是否缺少任何?)
也就是说,由于AttributeReference继承自DBText,因此可能有一种更优雅的方式。
  1. open Autodesk.AutoCAD.DatabaseServices
  2. open Autodesk.AutoCAD.EditorInput
  3. open Autodesk.AutoCAD.Geometry
  4. open Autodesk.AutoCAD.Runtime
  5. type acApp = Autodesk.AutoCAD.ApplicationServices.Application
  6. // Helper pattern to check for ObjectId RXClass
  7. let isClass (so: SelectedObject) =
  8.     RXClass.GetClass >> so.ObjectId.ObjectClass.IsDerivedFrom
  9. let (|IsText|IsInsert|Other|) so =
  10.     if isClass so typeof then IsText so.ObjectId
  11.     elif isClass so typeof[B] then IsInsert so.ObjectId
  12.     else Other
  13. // Handler for filtering
  14. let txBrFilter (e: SelectionAddedEventArgs) =
  15.     e.AddedObjects
  16.     |> Seq.cast
  17.     |> Seq.mapi
  18.         (fun i so -> match so with IsText _ | IsInsert _ -> None | _ -> Some i)
  19.     |> Seq.choose id
  20.     |> Seq.iter e.Remove
  21.                     
  22. // Extension for filter handler selection. Returns Some when exactly
  23. // two objects are selected
  24. type Editor with
  25.     member ed.TrySelectWithFilter (pso: PromptSelectionOptions) =
  26.         let obs = ed.SelectionAdded |> Observable.subscribe txBrFilter
  27.         let psr = ed.GetSelection pso
  28.         obs.Dispose()
  29.         match psr.Status with
  30.         | PromptStatus.OK when psr.Value.Count = 2 ->
  31.             Some(psr.Value.[0], psr.Value.[1])
  32.         | _ -> None
  33. // Static repository for user-supplied attribute tag
  34. // (accessible over multiple documents)
  35. let mutable tag = ""
  36. // Our command converts an existing DBText object to an AttributeReference
  37. // and attaches it to the AttributeCollection of an existing block reference.
  38. // If successful, the text will be erased
  39. []
  40. let Text2AttributeReference() =
  41.     // the usual suspects
  42.     let doc = acApp.DocumentManager.MdiActiveDocument
  43.     let ed = doc.Editor
  44.     let db = doc.Database
  45.     // Function for attribute tag input
  46.     let getAttTag() =
  47.         let pso =
  48.             new PromptStringOptions(
  49.                 "\nEnter attribute tag: ",
  50.                 AllowSpaces = false )
  51.         if tag  "" then
  52.             pso.DefaultValue  tag  ()
  53.     // Set up selection options and keyword. Select with filter
  54.     let pso = new PromptSelectionOptions(AllowDuplicates = false)
  55.     pso.Keywords.Add "attribute Tag"
  56.     pso.MessageForAdding  Observable.add (fun _ -> getAttTag())
  57.     let res = ed.TrySelectWithFilter pso
  58.               
  59.     // If not yet supplied, get the tag now
  60.     if tag = "" then getAttTag()
  61.    
  62.     // Evaluate user input
  63.     match res with
  64.     | Some(IsText tid, IsInsert bid)
  65.     | Some(IsInsert bid, IsText tid) when tag  "" ->
  66.         use tr = db.TransactionManager.StartTransaction()
  67.         let tx = tr.GetObject(tid, OpenMode.ForWrite) :?> DBText
  68.         let br = tr.GetObject(bid, OpenMode.ForWrite) :?> BlockReference
  69.         
  70.         // Iterate thru attribute collection and check for existing tag
  71.         let dupatt =
  72.             br.AttributeCollection
  73.             |> Seq.cast
  74.             |> Seq.exists
  75.                 (fun aid ->
  76.                     let ar =
  77.                         tr.GetObject(aid, OpenMode.ForRead)
  78.                             :?> AttributeReference
  79.                     ar.Tag = tag )
  80.                     
  81.         // If all is well, let's go
  82.         if not dupatt then
  83.             try
  84.                 let ar =
  85.                     new AttributeReference(
  86.                         Tag = tag,
  87.                         Color = tx.Color,
  88.                         Height = tx.Height,
  89.                         Justify = tx.Justify,
  90.                         Layer = tx.Layer,
  91.                         LinetypeId = tx.LinetypeId,
  92.                         LinetypeScale = tx.LinetypeScale,
  93.                         LineWeight = tx.LineWeight,
  94.                         Normal = tx.Normal,
  95.                         Oblique = tx.Oblique,
  96.                         Position = tx.Position,
  97.                         Rotation = tx.Rotation,
  98.                         TextString = tx.TextString,
  99.                         // TextStyle = tx.TextStyle,       // 2009
  100.                         TextStyleId = tx.TextStyleId,   // 2010
  101.                         WidthFactor = tx.WidthFactor )
  102.                 if tx.Justify  AttachmentPoint.BaseLeft then
  103.                     ar.AlignmentPoint  ignore
  104.                 tr.AddNewlyCreatedDBObject(ar, true)
  105.                 tx.Erase()
  106.             with ex ->
  107.                 "\nError creating attribute reference: " +
  108.                 ex.Message
  109.                 |> ed.WriteMessage
  110.         else
  111.             "\nAttribute tag "" + tag +
  112.             "" already used in block "" + br.Name + "" "
  113.             |> ed.WriteMessage
  114.         tr.Commit()
  115.     | Some _ ->
  116.         "\nSelect one block reference and one text object only. "
  117.         |> ed.WriteMessage
  118.     | _ -> ()

干杯!

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
1
发表于 2015-11-11 11:35:36 | 显示全部楼层
那么我如何从AutoCAD 2013环境中准确运行C#
。我尝试了大约5个代码,所有这些都出现了错误。这是我的最后手段,请帮忙....
回复

使用道具 举报

51

主题

613

帖子

9

银币

中流砥柱

Rank: 25

铜币
815
发表于 2015-11-11 12:08:07 | 显示全部楼层
http://www.learncs.org/
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-2-4 22:00 , Processed in 0.155299 second(s), 58 queries .

© 2020-2025 乐筑天下

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