Jump to content

Why does this code cause my model to regenerate twice?


sublimation

Recommended Posts

I can't figure out why when I manually enter the command it doesn't cause a regeneration of the model, but when I run my code, it causes it twice.

 

(defun C:PJ ()
	(get_CurrentState)
	(setvar "CMDECHO" 0)
	(setvar "PEDITACCEPT" 1)
	(COMMAND ".PEDIT" PAUSE "JOIN" "ALL" "" "")
	(cond
		((= (cdr (assoc 70 (entget (entlast)))) 1) (princ (strcat "\nPolyline: CLOSED		Vertices: " (itoa (cdr (assoc 90 (entget (entlast))))) "\n")))
		((= (cdr (assoc 70 (entget (entlast)))) 0) (princ "\nPolyline: OPEN\n"))
		)
	(set_CurrentState)
	(princ)
	)

It's not super critical that it happens, but it is certainly annoying. 

 

Any insight would be much appreciated.

 

image.png.a607334b0ea463186305c05e469ac54f.png

Link to comment
Share on other sites

2 minutes ago, Lee Mac said:

What are the definitions for these two functions?


get_CurrentState / set_CurrentState

 

 

Lee,

Here is the rest of the code.

 

(setq gbl_SysVars '(CMDECHO MENUECHO FILLETRAD OSMODE PDMODE PDSIZE COORDS BLIPMODE ORTHOMODE GRIDMODE PEDITACCEPT DYNMODE))


(defun get_CurrentState ()
	(setq gbl_SysValues
		(mapcar '(lambda (gbl_SysVars)
					(cons gbl_SysVars (getvar gbl_SysVars))
					)
			gbl_SysVars
			)
		)
	(princ)
	)

(defun set_CurrentState ()
	(mapcar '(lambda (gbl_SysValues)
				(setvar (car gbl_SysValues) (cdr gbl_SysValues))
				)
			gbl_SysValues
			)
	(princ)
	)

 

 

Link to comment
Share on other sites

This may not transpire to be the source of the regeneration issue that you are encountering, but you needn't set/reset so many system variables for each use of the program - you need only store/reset the values of those system variables being modified by the program.

 

Here is an example for you to try, based loosely on my Polyline Join program:

(defun c:pj ( / ent enx sel val var )
    (if
        (setq sel
            (ssget "_+.:E:S:L"
               '(
                    (-4 . "<OR")
                         (0 . "LINE,ARC")
                         (-4 . "<AND")
                             (0 . "LWPOLYLINE")
                             (-4 . "<NOT")
                                 (-4 . "&=") (70 . 1)
                             (-4 . "NOT>")
                         (-4 . "AND>")
                     (-4 . "OR>")
                )
            )
        )
        (progn
            (setq var '(cmdecho peditaccept)
                  val  (mapcar 'getvar var)
                  ent  (entlast)
            )
            (mapcar '(lambda ( a b c ) (if b (setvar a c))) var val '(0 1))
            (command "_.pedit" sel "_j" "_all" "" "")
            (mapcar '(lambda ( a b )   (if b (setvar a b))) var val)
            (if (and (not (eq ent (setq ent (entlast)))) (setq enx (entget ent)))
                (princ
                    (strcat
                        "\nPolyline: " (nth (logand 1 (cdr (assoc 70 enx))) '("OPEN" "CLOSED"))
                        "\tVerices: "  (itoa (cdr (assoc 90 enx)))
                    )
                )
                (princ "\nUnable to create polyline from selection.")
            )
        )
    )
    (princ)
)

 

  • Thanks 1
Link to comment
Share on other sites

 

Lee,

 

Thanks a lot!  That completely removed the regeneration issue and given me plenty to think about.

 

I've only been using Autolisp for a few weeks so I will have to study some of the techniques you used, but I have a question about one of them.  See below:

 

18 hours ago, Lee Mac said:

 


        (progn
            (setq var '(cmdecho peditaccept)
                  val  (mapcar 'getvar var)
 ----->>          ent  (entlast)
            )
            
            
 ----->>   (if (and (not (eq ent (setq ent (entlast)))) (setq enx (entget ent)))

 

Why is ent defined twice?  The code seemed to work fine without the first one definition.  Am I missing something?

 

 

 

Link to comment
Share on other sites

9 hours ago, sublimation said:

Thanks a lot!  That completely removed the regeneration issue and given me plenty to think about.

 

You're most welcome!

 

9 hours ago, sublimation said:

Why is ent defined twice?

 

Prior to evaluating the PEDIT command I assign the last primary entity in the drawing database to the symbol ent using the entlast function. Then, after calling the PEDIT command, I test whether the value held by the symbol ent points to the same entity as that returned by entlast (and I redefine the ent symbol at the same time).

 

If the entity is the same, this indicates that the PEDIT command has not added any entities to the drawing database and has hence failed to generate a new polyline; as such, the test expression for the if statement is only validated if these entities differ with the new entity assigned to ent being the new polyline generated by the PEDIT command.

Edited by Lee Mac
  • Like 2
Link to comment
Share on other sites

On 7/12/2019 at 5:29 PM, Lee Mac said:

Prior to evaluating the PEDIT command I assign the last primary entity in the drawing database to the symbol ent using the entlast function. Then, after calling the PEDIT command, I test whether the value held by the symbol ent points to the same entity as that returned by entlast (and I redefine the ent symbol at the same time).

 

If the entity is the same, this indicates that the PEDIT command has not added any entities to the drawing database and has hence failed to generate a new polyline; as such, the test expression for the if statement is only validated if these entities differ with the new entity assigned to ent being the new polyline generated by the PEDIT command.

 

Fascinating.  Thank you again!

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

×
×
  • Create New...