Get Properties Upon Selection

Get Properties Upon Selection

GeryKnee
Advocate Advocate
2,785 Views
6 Replies
Message 1 of 7

Get Properties Upon Selection

GeryKnee
Advocate
Advocate

Hello.

I need a lisp command doing the following:

When called,

  • If not selection exists, prompts “select an object”
  • If more than one object selected prompts “just one object expected”
  • If user cancels, halts
  • Sets the Current Color and Line Type to Bylayer .
  • Sets Current Layer to object’s layer
  • According to Current Unique Selected Object :
    1. If Selected Object is a Dim Object, sets the current dim style to the object’s dim style.
    2. If Selected Object is a polyline (or Rectangle) Object, the next time a new polyline (or Rectangle) Object is added the new created polyline (if width property is not defined by user) will have the (global) width property same as the selected polyline’s width property (the new polyline objects inherits width property of the selected polyline).
    3. If Selected Object is a Hatch Object, the next time a new Hatch Object is added (if hatch properties not defined by user ) the new created Hatch will inherit the selected ‘s properties.
    4. If Selected Object is a Text Object, the next time a new Text Object is added the new created Text will inherit the selected text‘s properties

All the above (and maybe everything else that maybe I forgot) say that this command will turn the last user defined initialization properties of a new created object according to those used (in the past) by user when the selected object create.

I gave it the name InhetitProp …

If everybody knows how to do, helps me.

I can’t write lisp codes.

Thanks,

Gery

0 Likes
2,786 Views
6 Replies
Replies (6)
Message 2 of 7

roland.r71
Collaborator
Collaborator

@GeryKnee wrote:

<snip>

I can’t write lisp codes.

Thanks,

Gery


You're in luck. This is the perfect opportunity to learn, and you're in the right place to find all the info you need. Smiley Happy

 

This will get you started:

(defun c:inheritProp ( / ss ent entType entLayer)

; --- DESCRIPTION --------------------------------------------------------------

; - Select single entity. Accept preselection, 1 object only
; - Current layer    = entity layer
; - Current color    = ByLayer
; - Current linetype = ByLayer
; - case of:
;   - Dimension  : set dimstyle as default
;   - Lwpolyline : set global width as default
;   - Hatch      : set props as default (hatch)
;   - Text       : set props as default (textstyle)

; --- MAIN ---------------------------------------------------------------------

; get entity
   (if (setq ss (ssget "i"))                                                    ; if there's a selection present
      (if (> 1 (sslength ss))							; if it has more then one
         (progn
            (princ "\nJust one object expected")
            (exit)                                                              ; exit
         )
         (setq ent (entget (nth 0 ss)))                                         ; preselected entity
      )
   )
   (if (= ss nil)
      (setq ent (entget (car (entsel))))                                        ; user selected entity
   )

; retrieve entity data
   (setq entType (cdr (assoc 0 ent)))                                           ; retrieve entity type
   (setq entLayer (cdr (assoc 8 ent)))                                          ; retrieve entity layer
   (command "_.-layer" "_s" entLayer "")                                        ; set entity layer as current
   (command "-linetype" "_s" "ByLayer" "")                                      ; set linetype ByLayer
   (command "-color" "ByLayer")                                                 ; set color ByLayer

   (princ (strcat "\nEntity is: " entType))
   (cond
      ((= entType "TEXT")
         ; retrieve & set text properties
         (princ "\nSync. current Textstyle")
      )
      ((= entType "LWPOLYLINE")
         ; retrieve & set global width
         (princ "\nSync. current Global Width")
      )
      ((= entType "HATCH")
         ; retrieve & set HatchPattern
         (princ "\nSync. current Hatch pattern")
      )
      ((= entType "DIMENSION")
         ; retrieve & set Dimstyle
         (princ "\nSync. current Dimension style")
      )
   )

; --- END ----------------------------------------------------------------------
   (princ)
)
Message 3 of 7

Kent1Cooper
Consultant
Consultant

I can do better than that....  Try MakeMore.lsp, with its MM command, available >here<.  Not only does it work in either verb-noun or noun-verb operation with limitation to a single object, and sets the various properties of a selected object current [for many more than your listing of object types or @roland.r71's offering], but it recognizes characteristics of certain things and offers default values.

 

For example, there's no such thing as a Rectangle entity type, but the RECTANG command makes a Polyline.  But so do a variety of other commands -- DONUT, POLYGON, REVCLOUD, sometimes BOUNDARY or SKETCH.  When you pick on any Polyline in MM, it offers all those possibilities to choose from [this is a big step up from AutoCAD's ADDSELECTED command, which just starts a basic PLINE command, no matter what kind of Polyline you pick on].  But if the Polyline is rectangular, it recognizes that, and offers RECTANG as the default kind of Polyline-making command, so you can just hit Enter and it will go into that, not simply into PLINE as ADDSELECTED does.  And it recognizes one made with DONUT, and a regular POLYGON, and an unaltered REVCLOUD, and offers those as default commands [while still offering all the other possibilities to choose from].

 

Similarly, if you pick a Text object, it offers the choice of whether to make a New one anywhere you want, or as a Continuation to a next one below the one you picked, as if you were still in the TEXT command that made it.  Or if you pick a Dimension object, it offers you the choice whether to make a New one anywhere, or use the Baseline or Continue options from the selected one.  And if you pick a Point object, it doesn't just go into a POINT command as ADDSELECTED does, but offers a choice between that and MEASURE and DIVIDE, either of which could have been the origin of the selected object.  Etc., etc.

Kent Cooper, AIA
Message 4 of 7

GeryKnee
Advocate
Advocate

dear Kent1Cooper

Thank you very match for your help now and so many times in the past.

I downloaded the SetAs lsp file.

It's near what i want, but

1) I don't want running the command to start creating a similar object that i wish to.

   I prefer, command to stop after setting the initialization system variables used as default

  properties when a new object is created getting the values from the sected object's properties   and nothing more.

2) Bylayer is my color and linetype option for all objects, so, initializing must reset my option

 

So i changed the code lines like this :::

 

 

(defun InheritProp_er (s)

  (if (/= s "function cancelled")

    (if    (= s "quit / exit abort")

      (princ)

      (princ (strcat "\nError:" s))

    )

  )

  (setvar "cmdecho" cmd)

  (setq *error* olderr)

  (princ)

)

 

(defun c:InheritProp ()

  (setq  olderr   *error*

            *error* InheritProp_er

  )

  (setq sel (entsel "\nSelect object: "))

  (if (null sel)

    (progn

      (princ (strcat "\n*Sorry no entity found*"))

      (setq sel (entsel "\nTry again select object: "))

      (if (null sel)

            (progn

              (princ

                (strcat "\nFor the scond time no entity has been selectd: ")

              )

              (exit)

            )

      )

    )

  )

  (setq cmd (getvar "cmdecho"))

  (setvar "cmdecho" 0)

  (setq ent (car sel))

  (setq edata (entget ent))

  (setq etype (cdr (assoc 0 edata)))

  (setq c (cdr (assoc 62 edata)))

  (setq lt (cdr (assoc 6 edata)))

  (setq l (cdr (assoc 8 edata)))

  (setq lts (cdr (assoc 48 edata)))

  (if (not lts)

    (setq lts 1)

  )

  ;;; ---------------- GENERAL

  (princ (strcat "\n " etype))

  (command "_color" "bylayer")

  (command "_linetype" "s" "bylayer" "")

  ;;;

  (command "_layer" "set" l "")

  (setvar "celtscale" lts)

  (if (eq etype "LINE")

    (progn

      (setq lts (cdr (assoc 48 edata)))                                                     ;

      (command "_ltscale" lts)

    )

  )

  ;;; ---------------- ARC

  (if (eq etype "ARC")

    (progn (setq lts (cdr (assoc 48 edata)))

               (command "_ltscale" lts)

    )

  )

  ;;; ---------------- CIRCLE

  (if (eq etype "CIRCLE")

    (progn (setq lts (cdr (assoc 48 edata)))

               (command "_ltscale" lts)

    )

  )

  ;;; ---------------- ELLIPSE

  (if (eq etype "ELLIPSE")

    (progn (setq lts (cdr (assoc 48 edata)))

               (command "_ltscale" lts)

    )

  )

  ;;; ---------------- SPLINE

  (if (eq etype "SPLINE")

    (progn (setq lts (cdr (assoc 48 edata)))

               (command "_ltscale" lts)

    )

  )

  ;;; ---------------- HATCH

  (if (eq etype "HATCH")

    (progn

      (setvar "HPNAME" (cdr (assoc 2 edata))) ;gets hatch pattern

      (setvar "HPSCALE" (cdr (assoc 41 edata)));gets hatch scale

      (setvar "HPANG" (cdr (assoc 52 edata)));gets hatch angle

    )

  )

  ;;; ---------------- LWPOLYLINE

  (if (eq etype "LWPOLYLINE")

    (progn

      (setq lts (cdr (assoc 48 edata)))

      (command "_ltscale" lts)

      (setq W1 (cdr (assoc 40 edata)))

      (setq W2 (cdr (assoc 41 edata)))

    )

  )

  ;;; ---------------- MLINE

  (if (eq etype "MLINE")

    (progn

    )

  )

  (if (eq etype "LEADER")

    (progn (setq st (cdr (assoc 3 edata)))

               (command "_dimstyle" "r" st)

    )

  )

  (if (eq etype "DIMENSION")

    (progn

      (setq dst (cdr (assoc 3 edata)))

      (command "_dimstyle" "r" dst)

    )

  )

  (if (eq etype "TEXT") 

    (princ)

    (progn (setq c (cdr (assoc 62 edata)));COLOR

               (setq l (cdr (assoc 8 edata)));LAYER

               (setq s (cdr (assoc 7 edata)));STYLE

               (setq f (cdr (assoc 4 edata)));FONT

               (setq h (cdr (assoc 40 edata)));HEIGHT

               (setq w (cdr (assoc 41 edata)));WIDTH

               (setq a (angtos (cdr (assoc 50 edata)) 0 4));ROTATION ANGLE

               (setq tx (cdr (assoc 1 edata)));text value

               (setq in (cdr (assoc 10 edata)));insertion point

               (setvar "cmdecho" 1)

               (command "erase" sel "")

               (command "_style" s "" h w "" "" "" "" "text" in a tx "dtext""")

    )

  )

  (if (eq etype "MTEXT")

    (princ)

    (progn (setq c (cdr (assoc 62 edata)));COLOR

               (setq l (cdr (assoc 8 edata)));LAYER

               (setq s (cdr (assoc 7 edata)));STYLE

               (setq f (cdr (assoc 4 edata)));FONT

               (setq h (cdr (assoc 40 s)));HEIGHT

               (setq w (cdr (assoc 41 edata)));WIDTH

               (setq a (angtos (cdr (assoc 50 edata)) 0 4));ROTATION ANGLE

               (setq in (cdr (assoc 10 edata)));insertion point

               (setvar "cmdecho" 0);

               (command "erase" sel "")

               (command "_style" s "" "" "" "" "" "" "")

    )

  )

  (setvar "cmdecho" 1)

  (setq *error* oldererr)

  (princ)

)

;end InheritProp.lsp

 

There are plenty of problems in what is written above.

Fo example ::

In "LWPOLYLINE" entity type section, W1 and W2 values do not change an autocad system variable. But if I start entering a polyline,  after 1st point is set, I get a prompt Specify next point or [Arc/Halfwidth/Length/Undo/Width]: Let me enter “W” The default value now is 0.00 I change it to 5.00. The next time I start a new polyline the default valuw for “W” is 5.00. Thas says to me that there’s a System value holding my optional value (5.00). That means, needed more code :::

  ;;; ---------------- LWPOLYLINE

  (if (eq etype "LWPOLYLINE")

    (progn

      (setq lts (cdr (assoc 48 edata)))

      (command "_ltscale" lts)

      (setq W1 (cdr (assoc 40 edata)))

      (command "_????? " W1)

Or >>> (setvar "?????"  W1)

      (setq W2 (cdr (assoc 41 edata)))

      (command "_????? " W2)

Or >>> (setvar "?????"  W2)    )

  )

The same for other Property values for other entity types set but not assigned to system variables.

 

In addition, MLINE entity type is empty

 

Sorry, my lisp knowledge is poor.

 

I write plenty of codes in Pascal and I maintain my AutoCAD extension using the AutoCAD Application OLE object and this all is friendly to me. But working with OLE Object is much slower than lisp codes because lisp is embedded to the AutoCAD environment. Lisp has a much different syntax than pascal and is very difficult to me, to change anything written in lisp.

 

I think can do the proper changes, so the command will work as I describe.

Thanks,

Gery.

 

 

 

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

@GeryKnee wrote:

....

I downloaded the SetAs lsp file.

It's near what i want, but.... 


Just to clarify -- SetAs is an older routine by someone else, linked to in the description in the post where MakeMore is.  I did MakeMore to overcome some shortcomings I saw in SetAsMakeMore could easily be altered to remove the calling of the command itself, if that's what you need [though I do wonder why you would want to, for example, set a Hatch's pattern name current if you don't then want to make another Hatch].

Kent Cooper, AIA
0 Likes
Message 6 of 7

GeryKnee
Advocate
Advocate

Hi ent1Cooper,

I answer your question.

What i want to do with this routine is to have a new option of Make Object's Layer Current version that sets values and to some else document variables holding the last used values of basic properties inherited from selected object.

I can easely do it using the AcadApp OLE using the object tools :

AcadApp.Doc.SetVariable(VariableName,Value);

VariableName is string

Value is either integer either string either float case to VariableName 

Variable name is for example

"CLAYER" for Current layer ......

I will try to how to with OLE

If you have information about list of all system variables with remarks about the value type and readonly state you'll help me.

thanks,

Gery

0 Likes
Message 7 of 7

Kent1Cooper
Consultant
Consultant

@GeryKnee wrote:

....

If you have information about list of all system variables with remarks about the value type and readonly state you'll help me. ....


Just press F1 for Help, and pick on this [may vary slightly in different versions]:
SysVars.PNG

Kent Cooper, AIA
0 Likes