Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
ShricharanaB
692 Views, 12 Replies

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

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.

Labels (4)
12 REPLIES 12
Message 2 of 13
ВeekeeCZ
in reply to: ShricharanaB

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.

Message 3 of 13
ShricharanaB
in reply to: ShricharanaB

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.

Message 4 of 13
ВeekeeCZ
in reply to: ShricharanaB

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.

Message 5 of 13
ВeekeeCZ
in reply to: ShricharanaB

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

Message 6 of 13
ShricharanaB
in reply to: ВeekeeCZ

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.

 

Message 7 of 13
ВeekeeCZ
in reply to: ShricharanaB


@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.

 

Message 8 of 13
ShricharanaB
in reply to: ВeekeeCZ

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
        )
    )
)

 

 

 

Message 9 of 13
ShricharanaB
in reply to: ShricharanaB

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. 

 

Message 10 of 13
ВeekeeCZ
in reply to: ShricharanaB

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.

 

Message 11 of 13
ВeekeeCZ
in reply to: ShricharanaB

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

 

Message 12 of 13
ShricharanaB
in reply to: ВeekeeCZ

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.
Message 13 of 13
ShricharanaB
in reply to: ВeekeeCZ

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. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta