Jump to content

Reactor Implementation Help Needed


sublimation
 Share

Recommended Posts

In the phases of learning something new, the most frustrating thing to me is when the examples make total sense, but you lack just enough understanding to implement it yourself.

I can read through Lee Mac's AssociativeCenterlines lisp, and it all makes sense, but I am missing something in my code (and thinking) to make these reactors fully work.

 

Below is the function where I attach the reactors to the objects and all of the reactor code I'm using. 

 

The erase reactor seems to work fine, though I wonder about reattaching the reactors afterwards.  I assumed doing that would help with the use of the UNDO command. Please correct me if I am wrong or if there is a better way.

 

The modification callback just seems to do nothing. What am I missing? Do I need a reactor for commandended too? 

 

(defun SUB:Generate_Dims (circ pt1 pta ptb / ctr dim circe dime)
	(setq ctr (vlax-get circ 'center)
		  circe (entget (vlax-vla-object->ename circ)))
	(setq dim (vla-adddimdiametric {MODELSPACE}
				(vlax-3d-point pta)
				(vlax-3d-point ptb)
				pt1
				)
		  dime (entget (vlax-vla-object->ename dim)))
	(vla-put-textoverride dim (strcat "{\\C5;N.NNNN\\P}<>"))
	(entmod
		(list (assoc -1 circe)
			(list -3
				(list wp:app
					(cons 1002 "{")
					(cons 1005 (vla-get-handle dim))
					(cons 1002 "}")
					)
				)
			)
		)
	(entmod
		(list (assoc -1 dime)
			(list -3
				(list wp:app
					(cons 1002 "{")
					(cons 1005 (vla-get-handle circ))
					(cons 1002 "}")
					)
				)
			)
		)
	(vlr-object-reactor (list circ) (list wp:app (vla-get-handle dim))
		(list
			(cons :vlr-erased 'wp:erase:callback)
			)
		)
	(vlr-object-reactor (list dim) (list wp:app (vla-get-handle circ))
		(list
			(cons :vlr-modified 'wp:text:callback)
			(cons :vlr-erased 'wp:erase:callback)
			)
		)
	)

;;; ==== REACTOR FUNCTIONS ====

(defun wp:erase:callback (owner reactor params / h en xR)
	(if
		(and
			(vlax-erased-p owner)
			(entget (setq en (handent (setq h (cadr (vlr-data reactor))))))
			)
		(progn
			(setq xR (vl-remove-if-not
						(function
							(lambda (x)
								(and
									(eq h (cadr (vlr-data x)))
									(eq wp:app (car (vlr-data x)))
									(equal '(:vlr-erased . wp:erase:callback) (car (vlr-reactions x)))
									)
								)
							)
						(cdar (vlr-reactors :vlr-object-reactor))
						)
				  )
			(mapcar 'vlr-remove (cons reactor xR))
			(entdel en)
			(mapcar 'vlr-add (cons reactor xR))
			)
		)
	(princ)
	)

(defun wp:text:callback (owner reactor params)
	(setq {data} (list owner reactor))
	(vlr-command-reactor (list wp:app)
		(list
			(cons :vlr-commandended     'wp:text:modify)
			(cons :vlr-commandcancelled 'wp:text:cancelled)
			(cons :vlr-commandfailed    'wp:text:cancelled)
			)
		)
	(vlr-remove reactor)
	(princ)  
	)

(defun wp:text:modify (reactor params / val mm c %)
	(vlr-remove reactor)
	(if
		(and
			{data}
			(not (vlax-erased-p (car {data})))
			(vlax-read-enabled-p (car {data}))
			(vlax-write-enabled-p (car {data}))
			(vlax-property-available-p (car {data}) 'TextOverride)
			)
		(progn
			(setq val (atof (substr (vl-string-right-trim "\\P}<>" (vla-get-textoverride (car {data}))) 6))
				  mm (vla-get-measurement (car {data}))
				  % (abs (/ (apply '- (list mm val)) (if (zerop mm) '0.001 mm)))
				  c (cond
						((or (zerop val) (>= % '1.0)) "7")
						((<= % '0.075) "3")
						((and (> % '0.075) (<= % '0.1)) "2")
						((> % '0.1) "1")
						(T "7")
						)
				  )
			(vla-put-textoverride (car {data}) (strcat "{\\C" c ";" (rtos val 2 3) "\\P}<>"))
			(vlr-add (cadr {data}))
			(setq {data} nil)
			)
		)
	(princ)
	)

(defun wp:text:cancelled (reactor params) 
	(vlr-remove reactor)
	(if {data}
		(progn
			(vlr-add (cadr {data}))
			(setq {data} nil)
			)
		)
	(princ)
	)

;;; ==== RESTORE REACTOR ASSOCIATION ====

(if (and wp:app (or (tblsearch "APPID" wp:app) (regapp wp:app)))
	((lambda (/ ent obj rX dt ss i xtyp xval)
		(foreach rX (cdar (vlr-reactors :vlr-object-reactor))
			(if (and
					(setq dX (vlr-data rX))
					(listp dX)
					(eq wp:app (car dX))
					)
				(vlr-remove rX)
				)
			)
		(if (setq ss (ssget "_X" (list '(0 . "DIMENSION") (list -3 (list wp:app)))))
			(repeat (setq i (sslength ss))
				(setq ent (ssname ss (setq i (1- i)))
					  obj (vlax-ename->vla-object ent)
					  xval (caddr (mapcar 'vlax-variant-value (vlax-safearray->list (progn (vla-getxdata obj wp:app 'xtyp 'xval) xval)))))
				(vlr-object-reactor (list obj) (list wp:app xval)
					(list
						(cons :vlr-modified 'wp:text:callback)
						(cons :vlr-erased 'wp:erase:callback)
						)
					)
				)
			)
		(if (setq ss (ssget "_X" (list '(0 . "CIRCLE") (list -3 (list wp:app)))))
			(repeat (setq i (sslength ss))
				(setq ent (ssname ss (setq i (1- i)))
					  obj (vlax-ename->vla-object ent)
					  xval (caddr (mapcar 'vlax-variant-value (vlax-safearray->list (progn (vla-getxdata obj wp:app 'xtyp 'xval) xval)))))
				(vlr-object-reactor (list obj) (list wp:app xval)
					(list
						(cons :vlr-erased 'wp:erase:callback)
						)
					)
				)
			)
		))
	)

 

As always, any help/tips/recommendations are greatly appreciated!

 

Edited by sublimation
Updated Code
Link to comment
Share on other sites

Well, I woke up this morning and the code suddenly made more sense to me.  I do need a commandended reactor, among other things.  I have revised the code above.

 

Now my new issue is if I modify multiple dimensions in a row, only the last one changed is updating.  But it's progress!

Link to comment
Share on other sites

I am still having problems implementing these reactors.  I can't figure out what I am missing.  With the code above, the only dimension that updates is the last one after I end the command.  My latest attempt gets stuck in a loop and causes autocad to crash. Could someone give me some guidance?

 

Latest attempt:

(defun wp:text:callback (owner reactor params / cRs)
	(setq {wp:data} (append (list (list owner reactor)) {wp:data}))
	(vlr-command-reactor (list wp:app)
		(list
			(cons :vlr-commandended     'wp:text:modify)
			(cons :vlr-commandcancelled 'wp:text:cancelled)
			(cons :vlr-commandfailed    'wp:text:cancelled)
			)
		)
	(vlr-remove reactor)
	(princ)  
	)

(defun wp:text:modify (reactor params)
	(vlr-remove reactor)
	(if {wp:data}
		(progn
			(mapcar
				(function
					(lambda (d / TO val mm c %)
						(if
							(and
								(not (vlax-erased-p (car d)))
								(vlax-read-enabled-p (car d))
								(vlax-write-enabled-p (car d))
								(vlax-property-available-p (car d) 'TextOverride)
								)
							(progn
								(setq TO (vla-get-textoverride (car d))
									  val (atof (substr (vl-string-right-trim "\\P}<>" TO) (if (wcmatch TO "[{\\]*,*C[0-7];*") 6 1)))
									  mm (vla-get-measurement (car d))
									  % (abs (/ (apply '- (list mm val)) (if (zerop mm) '0.001 mm)))
									  c (cond
											((or (zerop val) (>= % '1.0)) "7")
											((<= % '0.075) "3")
											((and (> % '0.075) (<= % '0.1)) "2")
											((> % '0.1) "1")
											(T "7")
											)
									  )
								(vla-put-textoverride (car d) (strcat "{\\C" c ";" (rtos val 2 3) "\\P}<>"))
								(vlr-add (cadr d))
								)
							)
						)
					)
				{wp:data}
				)
			(setq {wp:data} nil)
			)
		)
	(princ)
	)

(defun wp:text:cancelled (reactor params) 
	(vlr-remove reactor)
	(if {wp:data}
		(progn
			(mapcar (function (lambda (d) (vlr-add (cadr d)))) {wp:data})
			(setq {wp:data} nil)
			)
		)
	(princ)
	)

 

Edited by sublimation
Link to comment
Share on other sites

I suppose this:

(setq {wp:data} (append (list (list owner reactor) {wp:data})))

Should be:

(setq {wp:data} (append (list (list owner reactor)) {wp:data}))

 

  • Thanks 1
Link to comment
Share on other sites

15 hours ago, Roy_043 said:

I suppose this:


(setq {wp:data} (append (list (list owner reactor) {wp:data})))

Should be:


(setq {wp:data} (append (list (list owner reactor)) {wp:data}))

 

Nice catch! 

 

After being caught in a loop that added 34k reactors and crashed, the text did updated. Still not sure what I am missing.

Link to comment
Share on other sites

Took forever, but I finally figured it out.

Needed this:

(if (not (member (setq data (list owner reactor)) {wp:data}))
	(setq {wp:data} (cons data {wp:data}))
	)

added to my modify callback.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...