乐筑天下

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

[编程交流] Block's name to FIELD (ba

[复制链接]

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:17:43 | 显示全部楼层 |阅读模式
Hi there,
 
Another idea
 
Can anybody help me to modify existing lisp (by Lee Mac- lisp is great, thank you), please?
Right now lisp is creating text field with area value of selected hatch...
 
I would like to modify it so user can create 'label'=field with name value of selected block (dynamic and not dynamic). I was trying to change this code, but once again without success...
 
Lisp attached.
 
Thank you in advance.
 
Cheers,
Tom
a2f.lsp
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:23:19 | 显示全部楼层
So you want user to select a block and the field to display the block name? Is this at all related to the hatch?
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:24:30 | 显示全部楼层
Hi Lee Mac,
 
That was a quick replay
 
No is not related to the hatch, BUT. Before I managed to modify it so instead of hatch it was creating label with length value of selected pline...
I thought this will be similar as instead of referring to AcDbHatch Area- this will refer to something like AcDbBlock Name (I don't even know how to refer to blocks). I've tried that but is not working...
 
I have massive collection of blocks and I would like to create a 'label' next to each single one with name of the block... All blocks are in one cad file, that's why I need to see which block is which...
Of course there is no need to ask user about conversion factor or unit type here, only text height and pick block and pick point to create 'label=field'.
 
I'll really appreciate if you can help me!
 
Regards,
Tom
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:28:11 | 显示全部楼层
Try this:
 
  1. (defun c:FieldBlockName ( / *error* doc spc e p ) (vl-load-com) ;; © Lee Mac 2010 (defun *error* ( msg )   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")       (princ (strcat "\n** Error: " msg " **")))   (princ) ) (LM:ActiveSpace 'doc 'spc) (while   (and     (setq e       (LM:SelectifFoo         (lambda ( x )           (eq "INSERT" (cdr (assoc 0 (entget x))))         )         "\nSelect Block: "       )     )     (setq p (getpoint "\nPick Point for Field: "))   )   (LM:AddMText_MC spc p     (strcat "%%).EffectiveName>%"     )   ) ) (princ))(defun LM:AddMText_MC ( space pt str / o ) ;; © Lee Mac 2010 (setq o (vla-AddMtext space (vlax-3D-point pt) 0. str)) (vla-put-AttachmentPoint o acAttachmentPointMiddleCenter) (vla-put-InsertionPoint o (vlax-3D-point pt)))(defun LM:GetObjectID ( doc obj ) ;; © Lee Mac 2010 (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE")))   (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)   (itoa (vla-get-Objectid obj)) ));;--------------------=={ ActiveSpace }==---------------------;;;;                                                            ;;;;  Retrieves pointers to the Active Document and Space       ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  *doc - quoted symbol other than *doc                      ;;;;  *spc - quoted symbol other than *spc                      ;;;;------------------------------------------------------------;;(defun LM:ActiveSpace ( *doc *spc ) ;; © Lee Mac 2010 (set *spc   (if     (or       (eq AcModelSpace         (vla-get-ActiveSpace           (set *doc             (vla-get-ActiveDocument               (vlax-get-acad-object)             )           )         )       )       (eq :vlax-true (vla-get-MSpace (eval *doc)))     )     (vla-get-ModelSpace (eval *doc))     (vla-get-PaperSpace (eval *doc))   ) ));;-------------------=={ Select if Foo }==--------------------;;;;                                                            ;;;;  Continuous selection prompts until the predicate function ;;;;  foo is validated                                          ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  foo - predicate function taking ename argument            ;;;;  str - prompt string                                       ;;;;------------------------------------------------------------;;;;  Returns:  selected entity ename if successful, else nil   ;;;;------------------------------------------------------------;;(defun LM:SelectifFoo ( foo str / sel ent ) ;; © Lee Mac 2010 (while   (progn     (setq sel (entsel str))          (cond       (         (vl-consp sel)         (if (not (foo (setq ent (car sel))))           (princ "\n** Invalid Object Selected **")         )       )     )   ) ) ent)
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:30:38 | 显示全部楼层
I’m going to be honest with you Lee. You are GREAT!
 
  I have no idea how you are doing this, but this is absolutely fantastic!
 
  Thanks a lot again!
  Cheers,
  Tom
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:36:20 | 显示全部楼层
I have a question. I’ve tried to understand your code...
 
 
  I can’t see where you are referring to block (for picking object id). Before you were using AcDbHatch (for hatch id) but I can’t see this one now... Is it working for any object now and then depends what value you want to show, you are changing (i.e):
   
  1. (LM:GetObjectID doc (vlax-ename->vla-object e)) ">%).EffectiveName>%"
?
 
 
  Please see screenshots below.
    211745cy9dd43b9ux5498b.jpg
211750kl4orkvdrorrspee.jpg
  So my question is: if I would like to modify it so want to create label with coordinates of selected point, do I have to change only .EffectiveName to .Coordinates?
 
  Sorry for problems, but I want to understand it rather than bother you every single time
 
Thanks a lot!
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:39:34 | 显示全部楼层
Correct, you would just change the property that the field is pointing to. I check that the object is a block in the test statement for the WHILE function
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:39:59 | 显示全部楼层
And is working
 
I've changed
  1. (eq "[color=Red][b]INSERT[/b][/color]" (cdr (assoc 0 (entget x))))
to
  1. (eq "[color=Red]POINT[/color]" (cdr (assoc 0 (entget x))))
and EffectiveName to Coordinates and is creating label with coordinates of selected point
 
Now...
 
as you know there are some 'parameters' in
AcObjProp Object(%%).coordinates \f "%lu2%pr1">%
 
responsible for units, precision and other stuff...
 
Is there any way (and by any way I mean simply to understand, edit, change for other type i.e. from %pr to %tu)
to add some options for user to sett, as you did for lisp about hatches on top of this post.
I've tried to copy some code to this lisp to ask user to declare units and precision... of course without success.
 
So as you did with this code- I was able to understand most of it and change it so is picking different object and returning different value...
 
So let say for point coordinates, I want user to declare units and precision, so %lu and %pr but later I'll create other lisp (copy) and ask user to declare suffix %ps for length value of selected pline. I would like to know what to change and where (do I have to declare different variable for different stuff?) to be able to do that.  
 
Would it be possible to add this option to code, please?
 
I've done some programming in c++ years ago on uni, but I can't remember much. But at least I can understand some of your code instead of asking you every single time for new stuff.
 
Sorry for all those problems, but it's great to learn something from you!
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:45:18 | 显示全部楼层
Certainly, I'm happy that you are willing learn from the code, rather than use it blindly.
 
I'll post a generic example in a bit
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:49:14 | 显示全部楼层
Take a look at this Tom:
 
  1. (defun c:MakeField ( / *error* object property units prec pref suff conv doc spc e p ) (vl-load-com) ;; © Lee Mac 2010;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;;;                                                                               ;;;;                                Adjustments                                    ;;;;                                                                               ;;;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (setq object   "INSERT"         ;; Object Type (cannot be nil)       property "InsertionPoint" ;; Field Property (must belong to object, else field = #### )       units     2               ;; Units (integer: 1-6 or nil)       prec      3               ;; Precision (integer: 0-8 or nil)       pref     "Lee"            ;; Prefix (string or nil)       suff     "Mac"            ;; Suffix (string or nil)       conv      nil             ;; Conversion Factor (real or nil) );;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (defun *error* ( msg )   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")       (princ (strcat "\n** Error: " msg " **")))   (princ) ) (LM:ActiveSpace 'doc 'spc) (while   (and     (setq e       (LM:SelectifFoo         (lambda ( x )           (eq object (cdr (assoc 0 (entget x))))         )         (strcat "\nSelect " object ": ")       )     )     (setq p (getpoint "\nPick Point for Field: "))   )   (LM:AddMText_MC spc p     (strcat "%%)." property       (if (apply 'or (list units prec conv pref suff))         (strcat " \\f ""           (if units (strcat "%lu" (itoa units)) "")           (if prec  (strcat "%pr" (itoa prec )) "")           (if (or pref suff) (strcat "%ps[" (cond ( pref ) ( "" )) "," (cond ( suff ) ( "" )) "]") "")           (if conv  (strcat "%ct8[" (rtos conv) "]") "")           """         )         ""       )       ">%"     )   ) ) (princ))(defun LM:AddMText_MC ( space pt str / obj ) ;; © Lee Mac 2010 (if   (not     (vl-catch-all-error-p       (setq obj         (vl-catch-all-apply (function vla-AddMText)           (list space (vlax-3D-point pt) 0. str)         )       )     )   )   (progn     (vla-put-AttachmentPoint obj acAttachmentPointMiddleCenter)     (vla-put-InsertionPoint obj (vlax-3D-point pt))   ) ))(defun LM:GetObjectID ( doc obj ) ;; © Lee Mac 2010 (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE")))   (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)   (itoa (vla-get-Objectid obj)) ));;--------------------=={ ActiveSpace }==---------------------;;;;                                                            ;;;;  Retrieves pointers to the Active Document and Space       ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  *doc - quoted symbol other than *doc                      ;;;;  *spc - quoted symbol other than *spc                      ;;;;------------------------------------------------------------;;(defun LM:ActiveSpace ( *doc *spc ) ;; © Lee Mac 2010 (set *spc   (if     (or       (eq AcModelSpace         (vla-get-ActiveSpace           (set *doc             (vla-get-ActiveDocument               (vlax-get-acad-object)             )           )         )       )       (eq :vlax-true (vla-get-MSpace (eval *doc)))     )     (vla-get-ModelSpace (eval *doc))     (vla-get-PaperSpace (eval *doc))   ) ));;-------------------=={ Select if Foo }==--------------------;;;;                                                            ;;;;  Continuous selection prompts until the predicate function ;;;;  foo is validated                                          ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  foo - predicate function taking ename argument            ;;;;  str - prompt string                                       ;;;;------------------------------------------------------------;;;;  Returns:  selected entity ename if successful, else nil   ;;;;------------------------------------------------------------;;(defun LM:SelectifFoo ( foo str / sel ent ) ;; © Lee Mac 2010 (while   (progn     (setq sel (entsel str))          (cond       (         (vl-consp sel)         (if (not (foo (setq ent (car sel))))           (princ "\n** Invalid Object Selected **")         )       )     )   ) ) ent)
 
I have included quite a few 'adjustments' at the top of the code - obviously this doesn't include them all, but I wanted to give an idea..
 
Most error trapping regarding using the correct field code values is left to the user.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-11 09:10 , Processed in 1.149798 second(s), 74 queries .

© 2020-2025 乐筑天下

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