Author Topic: Reactor question  (Read 33873 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Reactor question
« on: February 14, 2006, 05:24:47 PM »
How do you tell if a reactor is associated to an object, when you only have the object?  I have one I'm working on, just a learning thing, and it seems to work well, but I want to be able to see if one is attached already.  I can post the code if needs be.  Right now it's just the basic associate a text object to a polyline.

Thanks for any help.  On second though, here is the code.
Code: [Select]
(defun c:AreaReact (/ Sel EntData PolyObj TextObj)

;|  Adds a presistant reactor to a polyline object that
    updates a selected text object to the polylines area
    in square feet.  You will have to have the subs loaded
    in everydrawing for it to work, so that it know what
    to do with the reactor, because it is saved with the
    drawing.  Saves the association between the text
    and the polyline in the extension dictionary of the
    polyline.
|;

(if
 (and
  (setq Sel (entsel "\n Select polyline to get area of: "))
  (setq EntData (entget (car Sel)))
  (= (cdr (assoc 0 EntData)) "LWPOLYLINE")
  (setq PolyObj (vlax-ename->vla-object (car Sel)))
  (setq Sel (entsel "\n Select text of hold area value: "))
  (setq EntData (entget (car Sel)))
  (vl-position (cdr (assoc 0 EntData)) '("TEXT" "MTEXT"))
  (setq TextObj (vlax-ename->vla-object (car Sel)))
 )
 (progn
  (PutArea PolyObj TextObj)
  (vlr-pers (vlr-object-reactor (list PolyObj) nil '((:vlr-modified . MakeCmdEndReactor))))
 )
)
(princ)
)
;---------------------------------------------------------------------------------------------------------------
(defun PutArea (PolyObj TextObj / Dict xRec SqFt)

(setq Dict (vla-GetExtensionDictionary PolyObj))
(if (vl-catch-all-error-p (setq xRec (vl-catch-all-apply 'vla-Item (list Dict "MyAreaReactor"))))
 (setq xRec (vla-AddXRecord Dict "MyAreaReactor"))
)
(MySetXrec xRec '(40 1) (list (vlax-get PolyObj 'Area) (vlax-get TextObj 'Handle)))
(setq SqFt (/ (vla-get-Area PolyObj) 144.0))
(vla-put-TextString TextObj (strcat (rtos SqFt 2 2) " SQ.FT."))
xRec
)
;----------------------------------------------------------------------------------------------------------------
(defun MakeCmdEndReactor (Obj React NotSure)

(setq GlbVarAreaObject Obj)
(if (not GlbReactorCommandEnd)
 (setq GlbReactorCommandEnd (vlr-command-reactor nil '((:vlr-commandEnded . AdjustTextObj))))
)
(princ)
)
;-----------------------------------------------------------------------------------------------------------------
(defun AdjustTextObj (React CommandList / Dict xRec xRecList TextObj)

(if
 (and
  GlbVarAreaObject
  (not (vlax-erased-p GlbVarAreaObject))
 )
 (progn
  (setq Dict (vla-GetExtensionDictionary GlbVarAreaObject))
  (if (not (vl-catch-all-error-p (setq xRec (vl-catch-all-apply 'vla-Item (list Dict "MyAreaReactor")))))
   (progn
    (setq xRecList (MyGetXRec xRec))
    (if
     (and
      (setq TextObj (vlax-ename->vla-object (setq tmpEnt (handent (cdr (assoc 1 xRecList))))))
      (not (vlax-erased-p TextObj))
     )
     (PutArea GlbVarAreaObject TextObj)
    )
   )
  )
  (setq GlbVarAreaObject nil)
 )
)
(setq GlbReactorCommandEnd nil)
)
;---------------------------------------------------------------------------
(defun MySetXRec (Obj CodeList DataList / )
; Sets XRecordData. Dxf numbers between 1-369, except 5, 100, 105.
; See help for types and numbers to use.

(vla-SetXRecordData Obj
 (vlax-make-variant
  (vlax-safearray-fill
   (vlax-make-safearray
    vlax-vbInteger
    (cons 0 (1- (length CodeList)))
   )
   CodeList
  )
 )
 (vlax-make-variant
  (vlax-safearray-fill
   (vlax-make-safearray
    vlax-vbVariant
    (cons 0 (1- (length Datalist)))
   )
   DataList
  )
 )
)
)
;-----------------------------------------------------------------------------
(defun MyGetXRec (Obj / CodeType DataType)
; Retrive XRecordData for an object

(vla-GetXRecordData
 Obj
 'CodeType
 'DataType
)
(if (and CodeType DataType)
 (mapcar
  '(lambda (a b)
   (cons a (variant-value b))
  )
  (safearray-value CodeType)
  (safearray-value DataType)
 )
)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Reactor question
« Reply #1 on: February 14, 2006, 05:29:54 PM »
No time to read all your code....

Here is a little function that might be useful:

Code: [Select]
(defun rwiz-partof  (obj / obj_reactors)
  (if (setq obj_reactors
     (cdar (vlr-reactors :vlr-object-reactor)))
    (vl-some
      (function (lambda (n) (numberp n)))
      (mapcar
(function (lambda (r) (vl-position obj (vlr-owners r))))
obj_reactors))))

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #2 on: February 14, 2006, 05:31:58 PM »
I guess a side question would be:
Is it safe to assume that the handles for objects won't change when opening and closing a drawing?  Right now on my test drawing, two polylines and two pieces of text, they dont' seem to change, but I'm not sure about large scale drawings.  I'm storing the handles (of the text objects) in the extension dictionary of the polyline objects so that I know which ones to update.

Thanks in advance.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #3 on: February 14, 2006, 05:33:55 PM »
Here is a little function that might be useful:
Thanks Luis.  I think that gave me a great idea how to do it.  Let you know what happens.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Reactor question
« Reply #4 on: February 14, 2006, 05:36:40 PM »
I guess a side question would be:
Is it safe to assume that the handles for objects won't change when opening and closing a drawing?  Right now on my test drawing, two polylines and two pieces of text, they dont' seem to change, but I'm not sure about large scale drawings.  I'm storing the handles (of the text objects) in the extension dictionary of the polyline objects so that I know which ones to update.

Thanks in advance.

You can use directly the built-in mechanism of VLR-PERS owners are saved in the (vlr-pers-dictname) = "VL-REACTORS"

LE

  • Guest
Re: Reactor question
« Reply #5 on: February 14, 2006, 05:40:01 PM »
The only problem of using VLR-PERS is that is not easy to know how to update the list of persistent reactors in vlr-pers-list...

LE

  • Guest
Re: Reactor question
« Reply #6 on: February 14, 2006, 05:44:27 PM »
Since I am in a good samaritan mode... and now I know that very few have downloaded and study what I did on this post:

http://www.theswamp.org/forum/index.php?topic=8327.0

I am going to removed the ZIP.... so if you want, grab it and look what I did to emulate what the VLR-PERS function does .... very simple mickey mouse solution... that really works and can be extended or make it better....  10, 9, 8, 7, 6,  :evil:  :roll:
« Last Edit: February 14, 2006, 05:50:02 PM by LE »

LE

  • Guest
Re: Reactor question
« Reply #7 on: February 14, 2006, 05:52:43 PM »
...5, 4, 3, 2, ......  :evil:  :lmao:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #8 on: February 14, 2006, 05:54:05 PM »
Since I am in a good samaritan mode... and now I know that very little have downloaded and study what I did on this post:

http://www.theswamp.org/forum/index.php?topic=8327.0

I am going to removed the ZIP.... so if you want, grab it and look what I did to emulate what the VLR-PERS function does .... very simple mickey mouse solution... that really works and can be extended or make it better....  10, 9, 8, 7, 6,  :evil:  :roll:
I was the first to test that one out.  You did it for me like a year or so ago on the Adesk help group.  You only could give it out as a vlx, so I couldn't learn from it at that time.  I thought it was still in vlx form, so I didn't grab it.  But thanks for letting me get it before you took it away.

I'm still trying to understand dictionaries, so I'm looking what you posted before and trying to grasp it.  Here is the little one I just wrote to see if an object has an object reactor associated to it.  Based on your quidence.  Whick I very much appreciate.  Thanks again.

Code: [Select]
(defun AssociatedReactors (Obj / ReactList)

(foreach i (vlr-reactors :vlr-object-reactor)
 (if (vl-position Obj (vlr-owners (cadr i)))
  (setq ReactList (cons (cadr i) ReactList))
 )
)
ReactList
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Reactor question
« Reply #9 on: February 14, 2006, 06:03:48 PM »
That was a joke...

The function looks good to me.... if the object is part of more than one...

I used my function, to test before passing the object as part of an object reactor.

The last object reactors I end up using VLR-PERS and handle everything inside of the "VL-REACTORS" dictionary...

Have fun!!!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #10 on: February 14, 2006, 06:06:20 PM »
That was a joke...

The function looks good to me.... if the object is part of more than one...

I used my function, to test before passing the object as part of an object reactor.

The last object reactors I end up using VLR-PERS and handle everything inside of the "VL-REACTORS" dictionary...

Have fun!!!
Thanks for the help, and the reviewal (not sure if it's a word, but sounds cool) of my code.  I think I'm getting the hang of it a little better.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Reactor question
« Reply #11 on: February 14, 2006, 06:16:07 PM »
Once, you have all your code done.... do this:

1. Open a drawing and run your code
2. Save an open it again, and see if the reactors are running, and call again your code to generate a new reactor, and save the drawing again.
3. Open that drawing and now, erase the polyline.... what happens?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #12 on: February 14, 2006, 06:22:51 PM »
Once, you have all your code done.... do this:

1. Open a drawing and run your code
2. Save an open it again, and see if the reactors are running, and call again your code to generate a new reactor, and save the drawing again.
3. Open that drawing and now, erase the polyline.... what happens?
It seems to be working fine.  But then again I have changed it, so that is there is an existing reactor associate to the polyline, that has the name of mine (new feature in the code, not posted) then it erases that one, and associates a new one.  Even the dictionary "VL-REACTORS" has only one for the count, when I opened the drawing it had two.

I'm still writing the code, but I can post it if you want.  I saw that I should associate and erase reactor to the object, but it seems like the way it is written now that it is not needed.  Am I missing something.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reactor question
« Reply #13 on: February 14, 2006, 06:26:57 PM »
I think I'm done.  If anyone wants to test it, be my guest, but same precautions as always, test in a test drawing, not a production drawing.  Let me know how it works for you.
Code: [Select]
(defun c:AreaReact (/ Sel EntData PolyObj TextObj ReactList Pos)

;|  Adds a presistant reactor to a polyline object that
    updates a selected text object to the polylines area
    in square feet.  You will have to have the subs loaded
    in everydrawing for it to work, so that it know what
    to do with the reactor, because it is saved with the
    drawing.  Saves the association between the text
    and the polyline in the extension dictionary of the
    polyline.
|;

(if
 (and
  (setq Sel (entsel "\n Select polyline to get area of: "))
  (setq EntData (entget (car Sel)))
  (= (cdr (assoc 0 EntData)) "LWPOLYLINE")
  (setq PolyObj (vlax-ename->vla-object (car Sel)))
  (setq Sel (entsel "\n Select text of hold area value: "))
  (setq EntData (entget (car Sel)))
  (vl-position (cdr (assoc 0 EntData)) '("TEXT" "MTEXT"))
  (setq TextObj (vlax-ename->vla-object (car Sel)))
 )
 (progn
  (PutArea PolyObj TextObj)
  (if
   (and
    (setq ReactList (AssociatedReactors PolyObj))
    (setq Pos (vl-position "MyAreaReactor" (mapcar 'vlr-data ReactList)))
   )
   (vlr-remove (nth Pos ReactList))
  )
  (vlr-pers (vlr-object-reactor (list PolyObj) "MyAreaReactor" '((:vlr-modified . MakeCmdEndReactor))))
 )
)
(princ)
)
;---------------------------------------------------------------------------------------------------------------
(defun PutArea (PolyObj TextObj / Dict xRec SqFt)

(setq Dict (vla-GetExtensionDictionary PolyObj))
(if (vl-catch-all-error-p (setq xRec (vl-catch-all-apply 'vla-Item (list Dict "MyAreaReactor"))))
 (setq xRec (vla-AddXRecord Dict "MyAreaReactor"))
)
(MySetXrec xRec '(40 1) (list (vlax-get PolyObj 'Area) (vlax-get TextObj 'Handle)))
(setq SqFt (/ (vla-get-Area PolyObj) 144.0))
(vla-put-TextString TextObj (strcat (rtos SqFt 2 2) " SQ.FT."))
xRec
)
;----------------------------------------------------------------------------------------------------------------
(defun MakeCmdEndReactor (Obj React NotSure)

(setq GlbVarAreaObject Obj)
(if (not GlbReactorCommandEnd)
 (setq GlbReactorCommandEnd (vlr-command-reactor nil '((:vlr-commandEnded . AdjustTextObj))))
)
(princ)
)
;-----------------------------------------------------------------------------------------------------------------
(defun AdjustTextObj (React CommandList / Dict xRec xRecList TextObj)

(if
 (and
  GlbVarAreaObject
  (not (vlax-erased-p GlbVarAreaObject))
 )
 (progn
  (setq Dict (vla-GetExtensionDictionary GlbVarAreaObject))
  (if (not (vl-catch-all-error-p (setq xRec (vl-catch-all-apply 'vla-Item (list Dict "MyAreaReactor")))))
   (progn
    (setq xRecList (MyGetXRec xRec))
    (if
     (and
      (setq TextObj (vlax-ename->vla-object (setq tmpEnt (handent (cdr (assoc 1 xRecList))))))
      (not (vlax-erased-p TextObj))
     )
     (PutArea GlbVarAreaObject TextObj)
    )
   )
  )
  (setq GlbVarAreaObject nil)
 )
)
(setq GlbReactorCommandEnd nil)
)
;---------------------------------------------------------------------------
(defun MySetXRec (Obj CodeList DataList / )
; Sets XRecordData. Dxf numbers between 1-369, except 5, 100, 105.
; See help for types and numbers to use.

(vla-SetXRecordData Obj
 (vlax-make-variant
  (vlax-safearray-fill
   (vlax-make-safearray
    vlax-vbInteger
    (cons 0 (1- (length CodeList)))
   )
   CodeList
  )
 )
 (vlax-make-variant
  (vlax-safearray-fill
   (vlax-make-safearray
    vlax-vbVariant
    (cons 0 (1- (length Datalist)))
   )
   DataList
  )
 )
)
)
;-----------------------------------------------------------------------------
(defun MyGetXRec (Obj / CodeType DataType)
; Retrive XRecordData for an object

(vla-GetXRecordData
 Obj
 'CodeType
 'DataType
)
(if (and CodeType DataType)
 (mapcar
  '(lambda (a b)
   (cons a (variant-value b))
  )
  (safearray-value CodeType)
  (safearray-value DataType)
 )
)
)
;-------------------------------------------------------------------------------------
(defun AssociatedReactors (Obj / ReactList)
; Return a list of reactors (object type) associated with an object.

(foreach i (vlr-reactors :vlr-object-reactor)
 (if (vl-position Obj (vlr-owners (cadr i)))
  (setq ReactList (cons (cadr i) ReactList))
 )
)
ReactList
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Reactor question
« Reply #14 on: February 14, 2006, 06:51:01 PM »
Tim;

Press F2 after opening the drawing where you have the areas [erase one of the plines that belongs to a reactor]

What is the error message?

Is this:
; warning:erased VLA-object restored to NIL