Unable to automate bedit command inside the block editor. AutoCAD 2020

ShricharanaB
Advocate
Advocate

Unable to automate bedit command inside the block editor. AutoCAD 2020

ShricharanaB
Advocate
Advocate

Hi, 

I'm replicating the bedit command to add an attribute check before actually calling the bedit command in autolisp. I've got almost everything working but when running bedit inside a block editor (BLOCKEDITOR=1) it asks for the keywords save or discard changes (to close it and then to open the block editor for the selected nested block. No matter what command keyword I give, it says unknown command or invalid keyword and switches to the default option selection with error. What am I doing wrong here?

I'm talking about these two options

ShricharanaB_0-1664618921520.png

 

 

 

(progn
 (command "_.-BEDIT" bname)
 (if (/= (getvar "BLOCKEDITOR") 0)
  (progn
   (initget 1 "Save Discard")
   (setq resp (getkword "\nSave or discard changes [Save/Discard]?"))
   (cond ((= resp "Save")
     (command "S"))
     ((= resp "Discard")
     (command "D"))
    )
  )
)

 

 Thanks in advance.

0 Likes
Reply
Accepted solutions (2)
887 Views
12 Replies
Replies (12)

ВeekeeCZ
Consultant
Consultant

I don't see any command name in your code to trigger this prompt. It could be BSAVE or BCLOSE... ?

 

I'm replicating the bedit command to add an attribute check before actually calling the bedit command in autolisp. I've got almost everything working but when running it inside a block editor (BLOCKEDITOR=1) it asks for the keywords save or discard changes. No matter what command keyword I give, it says unknown command or invalid keyword and switches to the default option selection with error. What am I doing wrong here?

I'm talking about these two options...

 

When, how?? What is the trigger? YOU need to control the trigger.

ShricharanaB
Advocate
Advocate

Hi, Thanks for looking into this.

The process is,

I have a block nested in a block. I open the main block for editing and select the nested block and run the bedit command again. That is when I get the prompt to save or discard ( only  if any changes are made, I think) before closing the block editor for current block (main block) and then open selected block for edit. Bedit gives that prompt. I'm just trying to automate this. 

 

It is related to this post actually. In the same way to refedit I'm replacing the bedit as well.

0 Likes

ВeekeeCZ
Consultant
Consultant
Accepted solution

Not sure whether you know ename or just bname (as block name). It would be easier if ename is known.

I don't know how to recognize whether were made changes (therefore the prompt comes up) or not. So the prompt must always there regardless this fact.

 

(progn
  (initget "Discard")
  (if (getkword "\nSave or discard changes? [Discard] <save>: ")
    (progn
      (sssetfirst nil (ssget "_X" (list (cons 2 bname)))) 
      (command "_.-bedit")
      (if (> (getvar 'cmdactive) 0) (command "_D")))
    (progn
      (command "_.bsave" "_.-bedit" bname)))) 

Edited.

ВeekeeCZ
Consultant
Consultant

Updated. Kinda forgot the second part of the original idea.

0 Likes

ShricharanaB
Advocate
Advocate

Hi, thank you for that snippet of code.

 

It works fine when I select save or press enter, but when I select Discard it opens an empty block named "undefine".

ShricharanaB_0-1664788775809.png

 

Edit: And bname is variable having effective name of the selected block gotten by

(setq bname (vla-get-effectivename obj))

where obj is the block object.

 

0 Likes

ВeekeeCZ
Consultant
Consultant

@ShricharanaB wrote:

Hi, thank you for that snippet of code.

 

It works fine when I select save or press enter, but when I select Discard it opens an empty block named "undefine".

 

Edit: And bname is variable having effective name of the selected block gotten by

(setq bname (vla-get-effectivename obj))

where obj is the block object.

 


 

Just made a quick test with regular blocks and it works fine. 

 

Did not try a dyn block but I would think that the actual name would be needed. Or get an ename.

 

ShricharanaB
Advocate
Advocate

Hi, 

 

Yes it does work for static blocks and dynamic blocks when they are in default state. As soon as I change a parameter of a nest dynamic block it stops working when I choose discard. I switched to Lee Mac's Effective Block Name function from my previous method (vlax-vla-object->ename obj) to see if that would work, still it doesn't. Now I'm confused if ename of a block is different than effective name? I have attached the test drawing file with a static block and a dynamic block within which is nested a static block within which is a dynamic block. When I check the value of Lee Mac's function I am getting the name of the block i.e "rec" ( the name I gave it) for the nested dynamic block. What am I missing ?

Lee Mac's function

 

 

;; Effective Block Name  -  Lee Mac
;; obj - [vla] VLA Block Reference object

(defun blkrst-effectivename ( obj )
    (vlax-get-property obj
        (if (vlax-property-available-p obj 'effectivename)
            'effectivename
            'name
        )
    )
)

 

 

 

0 Likes

ShricharanaB
Advocate
Advocate
Accepted solution

Thank you for the code! I really appreciate it! 

 

I was able to get it working with this little modification. Adding (command "_bclose" "_D") instead of (sssetfirst nil (ssget "_X" (list (cons 2 bname)))) .

(progn
   (initget "Discard")
   (if (getkword "\nSave or discard changes? [Discard] <save>: ")
      (progn		
         (command "_bclose" "_D")
         (command "_.-bedit" bname))
      (progn
            (command "_.bsave" "_.-bedit" bname))))

 

I'm still wondering if ename is different than effective name. Google search for ename only shows effective name. 

 

0 Likes

ВeekeeCZ
Consultant
Consultant

Does it also work when no modification is made so it does not offer the save/discard prompt at all?

 

Because that's the reason behind this,

    (progn
      (sssetfirst nil (ssget "_X" (list (cons 2 bname)))) 
      (command "_.-bedit")
      (if (> (getvar 'cmdactive) 0) (command "_D")))

 

The regular order is:

bclose

save/discard IF any change is made

blockname

 

Since we can't be sure whether the save/discard prompt pops up or not, I've used a block preselection basically to move the blockname specification before the save/discard prompt. Then I just ask Is the command is still active? -- if yes, it must be the save/discard prompt, so reply "discard". If not, no modification was made, no prompt.

 

ВeekeeCZ
Consultant
Consultant

1) ename as entity name.

<Entity name: 1f90fc2c670>.

It's paired with -1 code, but usually retrieved by some selection method or from related entities.

It's different when dwg is reopened. HERE 

 

2) bname as block name. circ in your case.

Paired with 2 code. 

 

Plenty of objects in AutoCAD are stored as blocks. Dimensions for example. They have generic anonymous names, see the list HERE 

 

Dynamic blocks are inserted with their original bname. When you change any of their dynamic params, the new changed geometry is saved under the *U# anonymous name (now this name is paired with code 2), and the original name refers to the original block definition referenced as the Effective name. 

 

Use the AutoCAD LIST command to see the actual block name. The first one before, the second after dyn-modification.

Command: LIST
Select objects: 1 found

Select objects:

                  BLOCK REFERENCE  Layer: "0"
                            Space: Model space
                   Handle = 7af
       Block Name: "poly"
                at point, X=5335.1912  Y= 888.8716  Z=   0.0000
   X scale factor:    1.0000
   Y scale factor:    1.0000
   rotation angle:      0
   Z scale factor:    1.0000
         InsUnits: Millimeters
  Unit conversion:    1.0000
  Scale uniformly: Yes
  Allow exploding: Yes
        Distance1: 2162.5733



Command: LIST

Select objects: Specify opposite corner: 1 found

Select objects:

                  BLOCK REFERENCE  Layer: "0"
                            Space: Model space
                   Handle = 7af
       Block Name: "poly"
   Anonymous Name: "*U10"
                at point, X=5335.1912  Y= 888.8716  Z=   0.0000
   X scale factor:    1.0000
   Y scale factor:    1.0000
   rotation angle:      0
   Z scale factor:    1.0000
         InsUnits: Millimeters
  Unit conversion:    1.0000
  Scale uniformly: Yes
  Allow exploding: Yes
        Distance1: 2410.7660

 

ShricharanaB
Advocate
Advocate
Does it also work when no modification is made so it does not offer the save/discard prompt at all?
> yes it is working for both cases. However, in my workstation I'm getting both save and discard options where the mouse is and in the commandline, but in another workstation I tried, the prompt is only in the command line (though still working as expected). Not sure what setting is causing that.

The second part - I understand, very clever.
0 Likes

ShricharanaB
Advocate
Advocate

ename - huh, I thought it was effectivename and was trying to use (vlax-vla-object->ename obj) conversion. No wonder it did not work.

I'll have to do a lot of experimentation to get a good grasp on Autolisp. This habit of jumping headfirst into coding what is needed teaches little it seems. 

Thank you for the clear explanation. 

0 Likes