TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Joker on March 14, 2006, 12:07:28 PM

Title: Get Block
Post by: Joker on March 14, 2006, 12:07:28 PM
I don’t know how to write a lisp.  But here is what I am looking to learn how to do.  I have an idea for a lisp the does the following.  Here is a description for your understanding: 

You have a block that is in a drawing and it does not exist in a Library.  With this lisp you would select the block and send it to a folder.  After you select the block that you want.  It maintains the name, the insertion point, colors, explodes the block, purges, sets it to layer 0 and sets it to 0,0.  If you need more information please feel free to ask.
Title: Re: Get Block
Post by: nivuahc on March 14, 2006, 12:15:03 PM
I don’t know how to write a lisp.  But here is what I am looking to learn how to do.  I have an idea for a lisp the does the following.

and there was much rejoicing  :-)

Here is a description for your understanding: 

You have a block that is in a drawing and it does not exist in a Library.  With this lisp you would select the block and send it to a folder.  After you select the block that you want.  It maintains the name, the insertion point, colors, explodes the block, purges, sets it to layer 0 and sets it to 0,0.  If you need more information please feel free to ask.


I'm fairly certain that there are a number of existing routines that pretty much do what you are looking for (mostly)... but you said you wanted to learn.

Do you have a time frame for this? Is this something that you have an idea for and want to use it as a learning spring-board or are you under pressure to have it in order to accomplish some task/project?

Other than that, the only question that I can think of is: Where would you like to begin?

There are plenty of members here who will be happy to help you learn to whatever level you are willing. I'm afraid I could only cover the basics, and I'd probably confuse you more than anything. There are far more elegant teachers here than I.
Title: Request for a Block Export Lisp
Post by: CAB on March 14, 2006, 12:35:26 PM
You probably already know this but wblock come close to doing what you want.

Quote
Command line  WBLOCK

To save a block or object as a separate drawing file

1   At the Command prompt, enter wblock.
2   In the Write Block dialog box, specify a block or an object to write out as a file.

Block: Specifies a block to save as a file.
   Entire Drawing: Selects current drawing as a block.
   Object: Specifies objects to be saved as a file.

3   Under Block, select a name from the list to save as a file.
4   Under Base Point, use the Pick Point button to define the base point.
5   Under Objects, use the Select objects button to select the object for the block file.
6   Enter a name for the new file.

If a block is selected, WBLOCK automatically uses that block's name for the new file.

7   In the Insert Units list, select an insert unit to use in AutoCAD DesignCenter.
8   Choose OK.

The block definition is saved as a drawing file.
Title: Re: Get Block
Post by: CAB on March 14, 2006, 12:41:31 PM
There is some great information to be found here as well. 
http://www.afralisp.co.za/
Title: Re: Get Block
Post by: Joker on March 14, 2006, 12:46:15 PM
You probably already know this but wblock come close to doing what you want.

Quote
Command line  WBLOCK

To save a block or object as a separate drawing file

1   At the Command prompt, enter wblock.
2   In the Write Block dialog box, specify a block or an object to write out as a file.

Block: Specifies a block to save as a file.
   Entire Drawing: Selects current drawing as a block.
   Object: Specifies objects to be saved as a file.

3   Under Block, select a name from the list to save as a file.
4   Under Base Point, use the Pick Point button to define the base point.
5   Under Objects, use the Select objects button to select the object for the block file.
6   Enter a name for the new file.

If a block is selected, WBLOCK automatically uses that block's name for the new file.

7   In the Insert Units list, select an insert unit to use in AutoCAD DesignCenter.
8   Choose OK.

The block definition is saved as a drawing file.

Yes, I know how to use the wblock coomand.
Title: Re: Get Block
Post by: Joker on March 14, 2006, 01:06:43 PM
There is some great information to be found here as well. 
http://www.afralisp.co.za/

I cannot access this link.  It locks my cumputer up!
Title: Re: Get Block
Post by: CAB on March 14, 2006, 01:10:06 PM
Quote
Yes, I know how to use the wblock coomand.
Good, so you see it does what you want except for the changing of the layers.

Let me say this is not a trivial lisp & not one to make your first lesson with.

My first though is to modify the existing block to set the layers the way you want
and then wblock it to the new drawing file. This would leave the existing drawing with
modified block. That is entities on layer zero & fixed colors. Is that OK?
We are not considering nested blocks here.

My second thought is to Insert a new copy of the block, explode it, collect the new objects
change them to layer zero & color to match previous layer. The wblock them out & delete them.

Neither method is a simple thing to do.

Perhaps someone can come up with a simpler solution.
Title: Re: Get Block
Post by: CAB on March 14, 2006, 01:13:38 PM
I cannot access this link.  It locks my cumputer up!

OK, change the link. Try again.
Title: Re: Get Block
Post by: T.Willey on March 14, 2006, 01:23:12 PM
I don't think it should be that hard.  Insert the block (save the point, and set it to a variable using entlast).  Explode the block, step through all the entity with entnext, or if you use vla-Explode (it will put them all in a list).  Change all the entities to the way you want them.  And then wblock out the explode pieces, with the insertion point you saved.

Would this work?
Title: Re: Get Block
Post by: nivuahc on March 14, 2006, 02:40:23 PM
Actually, as daunting a task as this may seem to a beginner, there are some valuable core lessons that could be learned, one step at a time, if it's taught properly. Start simple, then make it complex.

For example: Lesson 1

Code: [Select]
(defun c: lesson_1 ()
  (ssget...
    (command...
    )
   )
  )
 
Then, once all of that is absorbed, move on to bigger and better things
Title: Re: Get Block
Post by: CAB on March 14, 2006, 05:16:42 PM
I don't think it should be that hard.  Insert the block (save the point, and set it to a variable using entlast).  Explode the block, step through all the entity with entnext, or if you use vla-Explode (it will put them all in a list).  Change all the entities to the way you want them.  And then wblock out the explode pieces, with the insertion point you saved.

Would this work?
Tim read this http://tinyurl.com/kqaez

Not that big a deal though, Jeff has a nice routine for collecting the exploded block.

Well how about some pseudo code?

Title: Re: Get Block
Post by: CAB on March 14, 2006, 05:26:57 PM
My pseudo code.
Code: [Select]
User picks block to export
Insert the block at 0,0
Explode the block
Collect the new entities in a list
Change all the entities to color of layer if color ByLayer
Change all the entities to layer zero
wblock out the explode pieces, ? any user input ?
Delete the explode pieces

And this is the first lisp joker will write, so be through in your expiation. :-)
Title: Re: Get Block
Post by: T.Willey on March 14, 2006, 05:27:39 PM
Tim read this http://tinyurl.com/kqaez

Not that big a deal though, Jeff has a nice routine for collecting the exploded block.

Well how about some pseudo code?



I have read that, and am disappointed in the fact that it doesn't work correctly.  Its a good read for others who don't know about that fault of vla-Explode.  Thanks for posting it here.

Code..... Maybe
Code: [Select]
(defun c:ReBlock ()
Select the block to be reblocked.
Insert said block, with scale values of 1 in all directions (save point, and name)
 note: with insertion block, and having scaling of all 1's, then you can use vla-Explode on it
Explode said block (one way or another, whatever you are comfortable with)
Step through each object, and change the properties, and add to a list (be careful about objects with subentities: blocks and old style polylines)
Wblock out all items in the list, with the name from before, and the insertion point
)

Or did you mean to write the actually code?
Title: Re: Get Block
Post by: CAB on March 14, 2006, 05:35:20 PM
No you got it,  just pseudo code.

Here is a bit of code I got from somewhere.
It will take care of these two:
Quote
Explode the block
Collect the new entities in a list
Code: [Select]
(setq lst
   (mapcar
      'vlax-vla-object->ename
       (vlax-safeArray->list
          (vlax-variant-value
             (vla-Explode (vlax-ename->vla-object ename))
          )
       )
   )
)
Title: Re: Get Block
Post by: T.Willey on March 14, 2006, 05:46:21 PM
No you got it,  just pseudo code.

Here is a bit of code I got from somewhere.
It will take care of these two:
Quote
Explode the block
Collect the new entities in a list
Code: [Select]
(setq lst
   (mapcar
      'vlax-vla-object->ename
       (vlax-safeArray->list
          (vlax-variant-value
             (vla-Explode (vlax-ename->vla-object ename))
          )
       )
   )
)

You could just do..
Code: [Select]
(setq EntList
 (mapcar
  'vlax-vla-object->ename
  (vlax-invoke (vlax-ename->vla-object Ent) 'Explode)
 )
)

Also remember that with vla-Explode, you leave the original object in place.
Title: Re: Get Block
Post by: Joker on March 14, 2006, 06:16:59 PM
I did a search and found out what pseudo code is.  If I understand it correctly, Pseudo Code is a shopping list of tasks that the program needs to do in order to get what you want.

Here is my pseudo code:
Code: [Select]
User picks block to export
The block maintains the name
Export the block to new location
Maintain insert point
Explode the block
Collect all new entities in a list
Set to layer 0
Maintain colors the block provides
Set linetype to By Block
Insert Block to 0,0
Purge
Save
close
End of pseudo code
Title: Re: Get Block
Post by: T.Willey on March 14, 2006, 06:30:21 PM
You are correct about the term pseudo code.  We are giving you the pseudo code so you can do some research to learn how to do it.  Take what we give you and run with it, and if you have questions, post back and someone will answer it for you.  You stated that you want to learn with this one, so this is you chance to get started, or do you need a push to help you out?
Title: Re: Get Block
Post by: Joker on March 14, 2006, 06:44:03 PM
You are correct about the term pseudo code.  We are giving you the pseudo code so you can do some research to learn how to do it.  Take what we give you and run with it, and if you have questions, post back and someone will answer it for you.  You stated that you want to learn with this one, so this is you chance to get started, or do you need a push to help you out?

Could you please give me a push in the right direction?  Once I get pushed in the right direction and start to figure it out.  I will be back with questions and so on.
Title: Re: Get Block
Post by: T.Willey on March 14, 2006, 06:51:15 PM
Could you please give me a push in the right direction?  Once I get pushed in the right direction and start to figure it out.  I will be back with questions and so on.
My pleasure.

First off I would select the block to exported from the screen.  Either use "ssget" with a filter list for only blocks, or use "entsel" and then make sure you have a block.  For both the filter list, and to make sure you have a block, you will need to look at the dxf code.  You can access this from the help files.  Once you select the object, you can get the entity data of the object with "entget".  From there you can get the block name (dxf code 2).  This should be enough to get you started.  Have fun.  You can also search for little tidbits of help if no one is on to help you out.

Have fun!
Title: Re: Get Block
Post by: nivuahc on March 14, 2006, 07:13:06 PM
Code: [Select]
User picks block to export
The block maintains the name
Export the block to new location
Maintain insert point

I would start there and build on it. That's just me. Too much information tends to overwhelm. YMMV.
Title: Re: Get Block
Post by: Jim Yadon on March 14, 2006, 07:25:23 PM
To start with, my LISP skills are limited. So please just take this as an observation.

Did you all consider that some folks may have entities already existing at or in the vicinity of 0,0? You could avoid the potential hazards of picking up extra 'passengers' when the block gets re-written.

Code: [Select]
User picks the block to export.
Pull the object from the selection set to the clip board.
Open a new drawing.
Paste to drawing.
Acquire insertion point info.
Explode.
Clean.
Purge.
Rewrite to a new file maintaining insertion point info.
Close new session.
Title: Re: Get Block
Post by: Jeff_M on March 14, 2006, 08:24:58 PM
Rather than insert & explode, just set an Undo Begin, get the block name from a selection, get the block object from the block table, edit the block objects direct for color, linetype & layer, then Wblock out the block....bam, yer done. Then set Undo End then Undo Back.

Just my $0.02
Title: Re: Get Block
Post by: Jim Yadon on March 14, 2006, 09:03:59 PM
Rather than insert & explode, just set an Undo Begin, get the block name from a selection, get the block object from the block table, edit the block objects direct for color, linetype & layer, then Wblock out the block....bam, yer done. Then set Undo End then Undo Back.

Just my $0.02

Out of curiousity, why would you put the UNDO code in there? I don't see why you'd need it at that point.
Title: Re: Get Block
Post by: Jim Yadon on March 14, 2006, 09:18:32 PM
Because the Block table has been modified, and you dont want that change to persist.

That's why I am asking. It would seem to me that if you were going to the trouble of stripping a block down to the bones (aka zero layering it) to write it to file for later use, that you'd want those changes to persist in the drawing that it's already existing/being used in. Hence, ' why the UDO? '. Make sense?
Title: Re: Get Block
Post by: Jim Yadon on March 14, 2006, 09:34:11 PM
Sorry. I got so hung up on trying to understand the undo that your notes didn't sink in.  :ugly:
Title: Re: Get Block
Post by: Jeff_M on March 14, 2006, 10:19:49 PM
Jay,
I wouldn't use the Undo in my rendition of this for the very reasons you pointed out. But as Kerry also pointed out, that's pretty much what the OP requested.
Title: Re: Get Block
Post by: CAB on March 14, 2006, 11:36:30 PM
Jay,
I wouldn't use the Undo in my rendition of this for the very reasons you pointed out. But as Kerry also pointed out, that's pretty much what the OP requested.

That is the way I understood the request but I'm nit sure that is the desired result. joker will need to clarify that point for us.
I personally would keep the changes in the parent drawing.
Title: Re: Get Block
Post by: CAB on March 14, 2006, 11:40:35 PM
Rather than insert & explode, just set an Undo Begin, get the block name from a selection, get the block object from the block table, edit the block objects direct for color, linetype & layer, then Wblock out the block....bam, yer done. Then set Undo End then Undo Back.

Just my $0.02
When you say "wblock out the block" are you actually saying "wblock out the insert"? Just want to have it clear in my mind.
If you are wblocking out the insert, what if it is scaled and/or rotated?
Title: Re: Get Block
Post by: CAB on March 15, 2006, 12:28:26 AM
I believe Jeff meant "duplicate the definition", which is the way I also read the request.
Kerry could I trouble you to clarify that?
By definition you mean the 'Block definition in the block table' and not the Insert.
By duplicate you mean create a new definition in the block table?
Title: Re: Get Block
Post by: CAB on March 15, 2006, 12:44:07 AM
While the smoke is clearing, one more step.
If the insert is scaled and or rotated the we should not use that object to duplicate.
So to wblock you can not use a 'Block Definition', you need an Insert?
Therefore we are back to Insert & Explode?
Title: Re: Get Block
Post by: CAB on March 15, 2006, 01:07:43 AM
Good Night. :-)
Title: Re: Get Block
Post by: Slim© on March 15, 2006, 01:10:46 AM
:lmao:

Can I say "it depends", and get away with it ?

Only if you're "John Glenn". (http://www.cheesebuerger.de/images/midi/figuren/d030.gif)(http://www.thetechlounge.com/forum/images/smilies/more_smilies/drevil1.gif)
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 02:17:08 AM
That is why you don't insert the block, but just get the name of the block from the drawing, and the get the definition from the block table (collection), and copy all the objects from there to the new drawing.  Create a blank drawing with the name being the same as the block, and then open it with ObjectDBX and copy from the base drawing into the new one.
Title: Re: Get Block
Post by: Jeff_M on March 15, 2006, 02:58:33 AM
Tim, while that is one way to do this (although creating it first then opening with ObjectDBX is not necessary....I'll let you think on that ;-) ) I think it is beyond the scope of a beginner program which is why I suggested wblock. With wblock you can specify a Block name to export and it will take the BlockDefinition and create a new drawing with the name specified.

Alan, I would think that for a block Library, which was mentioned in the first post, you'd want the blockdef not an inserted version of it. Otherwise you'd want to rename it accordingly for the different variations that can be made from it.

I gotta remember that Kerry is up while the rest of us should be sleeping..... :kewl:
Title: Re: Get Block
Post by: CAB on March 15, 2006, 08:45:28 AM
Kerry,
Well I seldom use wblock & had allways select an object, not realizing the block NAME used the block definition. That does make matter simpler. Thanks Kerry for the code which explains everything. Now do you want to explain that to someone who has never written a lisp? :)

I'm waiting to see if Tim can come up with Jeff's riddle, as I don't have the answer. 8-)
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 11:08:29 AM
Tim, while that is one way to do this (although creating it first then opening with ObjectDBX is not necessary....I'll let you think on that ;-) ) I think it is beyond the scope of a beginner program which is why I suggested wblock. With wblock you can specify a Block name to export and it will take the BlockDefinition and create a new drawing with the name specified.
I'm waiting to see if Tim can come up with Jeff's riddle, as I don't have the answer. 8-)
Give me a minute.  I need to get my thinking cap on.

I coded one up real fast yesterday with the insert explode wblock out, all with ActiveX methods, but was getting some kind of error, and I had to go home, so I couldn't work on it anymore.
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 11:17:07 AM
.................  the code which explains everything. Now do you want to explain that to someone who has never written a lisp? :)


I had thought that the Joker may like to attempt to explain the code.  ... I can't think of a better way to learn, one line at a time.
Maybe you can tell him where to get the information, since you used (vlax.... stuff.  I know lispers that don't know where to get that info.

Okay... I will tell..
Click help
Click AutoLISP, Visual LISP, and DXF
Then on the right side click "AutoLISP Reference"
Then go to ActiveX and VBA Reference.

That is how I do it.
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 11:31:53 AM
Tim, while that is one way to do this (although creating it first then opening with ObjectDBX is not necessary....I'll let you think on that ;-) )
The only way I can think of right now that might work is to do it the way I did it in my code yesterday.  Use the WBlock method on the active document for all the objects in the blocks definition.  I can't think of a way to use ObjectDBX any other way.

... or, load it into the VLIDE, select any command you want help with, press CTRL+F1 .. there are only about 20 functions used in that routine.


and "The Visual Lisp Developer's Bible" by David M. Stein .. I think Jeff has a copy on this site somewhere, or google it, or look for it at Customer Files on the AutoDesk Newsgroups

Good point.  I don't use the vlide, so I wouldn't have thought of that.  I code in notepad.
Title: Re: Get Block
Post by: Jeff_M on March 15, 2006, 11:58:59 AM
Tim, while that is one way to do this (although creating it first then opening with ObjectDBX is not necessary....I'll let you think on that ;-) )
I can't think of a way to use ObjectDBX any other way.
HINT - Create an Instance of ObjectDBX...
Code: [Select]
(setq odbx (vla-GetInterfaceObject *acad* "ObjectDBX.AxDbDocument.16") )
now use (vlax-dump-object odbx t)......look familiar? If not, compare it to a dump of the ActiveDocument......

BTW, using the VLIDE makes it real easy to check these things on the fly.
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 12:25:34 PM
Tim, while that is one way to do this (although creating it first then opening with ObjectDBX is not necessary....I'll let you think on that ;-) )
I can't think of a way to use ObjectDBX any other way.
HINT - Create an Instance of ObjectDBX...
Code: [Select]
(setq odbx (vla-GetInterfaceObject *acad* "ObjectDBX.AxDbDocument.16") )
now use (vlax-dump-object odbx t)......look familiar? If not, compare it to a dump of the ActiveDocument......

BTW, using the VLIDE makes it real easy to check these things on the fly.
I still don't see it oh Master of the ObjectDBX...... Teach me so that I might learn the secrets of it also.

I know that T.T. did something with ObjectDBX to clone a block in the same drawing, but I never looked at the code.
Title: Re: Get Block
Post by: Jeff_M on March 15, 2006, 01:31:23 PM
OK, here ya go Tim.
Code: [Select]
(defun c:myWblock (/ *acad *doc bname bpath ent_list lay ms odbx)
  (if (and (setq *acad (vlax-get-acad-object))
   (setq *doc (vla-get-activedocument *acad))
   (setq bname (getstring "\nBlock name to save to Library: "))
   (tblsearch "BLOCK" bname)
   (setq bPath (findfile (getstring t "\nLibrary Path: ")))
   )
    (progn
      (setq odbx (if (< (atoi (substr (getvar "acadver") 1 2)) 16)
   (vla-GetInterfaceObject *acad "ObjectDBX.AxDbDocument")
   (vla-GetInterfaceObject *acad "ObjectDBX.AxDbDocument.16"))
    )
      (vlax-for ent (vla-item (vla-get-blocks *doc) bname)
(setq ent_list (cons ent ent_list))
)
      (setq ent_list (reverse ent_list))
      (vlax-invoke *doc 'copyobjects ent_list (setq ms (vla-get-modelspace odbx)) 'pairs)
      (vlax-for ent ms
(setq lay (vla-item (vla-get-layers odbx) (vla-get-layer ent)))
(vla-put-color ent (vla-get-color lay))
(vla-put-linetype ent "ByBlock")
(vla-put-layer ent "0")
)
      (vlax-for lay (vla-get-layers odbx)
(vl-catch-all-apply '(lambda ()
       (vla-delete lay)
       ))
)
      (vla-saveas odbx (strcat bPath "\\" bname ".dwg"))
      (vlax-release-object odbx)
      (vlax-release-object *doc)
      (vlax-release-object *acad)
      )
    )
  (princ)
  )
Title: Re: Get Block
Post by: T.Willey on March 15, 2006, 01:39:58 PM
Okay.  My wording was wrong.  I would have done it the same way.  Good code Jeff, as always.

Since everyone is posting code, I guess I will post mine also. :evil:
Right now the block path is hard coded to "C:\temp\".
Code: [Select]
(defun c:ExportBlock (/ ActDoc CurSpace ss Ent BlkName BlkNameList tmpObj tmpss tmpObjList BlkDir FullPath)

(vl-load-com)
(setq BlkDir "C:/temp/")
(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(setq CurSpace
 (vlax-get
  ActDoc
  (if (equal (getvar "cvport") 1)
   'PaperSpace
   'ModelSpace
  )
 )
)
(setq ssCol (vla-get-SelectionSets ActDoc))
(if (setq ss (ssget '((0 . "INSERT"))))
 (while (setq Ent (ssname ss 0))
  (setq BlkName (cdr (assoc 2 (entget Ent))))
  (if
   (and
    (not (vl-position BlkName BlkNameList))
    (/= (substr BlkName 1 1) "*")
   )
   (progn
    (setq BlkNameList (cons BlkName BlkNameList))
    (setq tmpObj
     (vlax-invoke
      CurSpace
      'InsertBlock
      '(0.0 0.0 0.0)
      BlkName
      1.0
      1.0
      1.0
      0.0
     )
    )
    (setq tmpss (vla-Add ssCol "tmpss"))
    (foreach Obj (setq tmpObjList (vlax-invoke tmpObj 'Explode))
     (vla-put-Layer Obj "0")
     (vla-put-Color Obj acByBlock)
     (vla-put-Linetype Obj "ByBlock")
    )
    (vla-AddItems tmpss
     (vlax-make-variant
      (vlax-safearray-fill
       (vlax-make-safearray vlax-vbObject (cons 0 (1- (length tmpObjList))))
       tmpObjList
      )
     )
    )
    (if (findfile (setq FullPath (strcat BlkDir BlkName ".dwg")))
     (progn
      (initget "Yes No")
      (if (/= (getkword (strcat "\n Block \"" BlkName "\" aready exist.  Wish to overwrite it [<Y>es No]: ")) "No")
       (vlax-invoke ActDoc 'WBlock FullPath tmpss)
      )
     )
    )
    (foreach Obj tmpObjList
     (vla-Delete Obj)
    )
    (vla-Delete tmpObj)
    (vla-Delete tmpss)
    (setq tmpss nil)
   )
  )
  (ssdel Ent ss)
 )
)
(if BlkNameList
 (progn
  (textscr)
  (prompt "\n - Created drawings in \"C:\\Temp\\\" for following blocks: ")
  (foreach i BlkNameList
   (prompt (strcat "\n  " i))
  )
 )
)
(princ)
)
Title: Re: Get Block
Post by: Jeff_M on March 15, 2006, 04:14:49 PM
I'm waiting to see if Tim can come up with Jeff's riddle, as I don't have the answer. 8-)
Alan, in case you still don't see what I was alluding to:
When you create an ObjectDBX object, it IS a new blank drawing (and I do mean Blank, as I'm pretty sure it does not use any template) so you can immediately just copy the block objects into the modelspace of that document and save. This now gives you the drawing you need without having to create, save, open in ObjectDBX & copy the objects into it, as Tim was saying.

IOW, since Tim was referencing ObjectDBX for a portion of the job, I was just pointing out that it could be used for the entire job.
Title: Re: Get Block
Post by: CAB on March 15, 2006, 04:33:55 PM
Thanks Jeff, that is good to know. That ObjectDBX stuff is still quite the mystery to me.
Title: Re: Get Block
Post by: Joker on March 16, 2006, 01:10:45 PM
You guys are moving faster than I I can figure things out.
Title: Re: Get Block
Post by: nivuahc on March 16, 2006, 01:15:41 PM
Code: [Select]
User picks block to export
The block maintains the name
Export the block to new location
Maintain insert point

I would start there and build on it. That's just me. Too much information tends to overwhelm. YMMV.
Title: Re: Get Block
Post by: Slim© on March 16, 2006, 01:19:15 PM
You guys are moving faster than I I can figure things out.

Samething is always happening to me, Joker. I just try to read and put to use what the nice folks around here put forward. If we seem a bit short, we're working at the same time. Just relax and take it at your own pace, ask questions, keep a positive attitude, and take most comments with a grain of salt.  :roll:

Enjoy theSwamp!!!
Title: Re: Get Block
Post by: T.Willey on March 16, 2006, 01:28:14 PM
You guys are moving faster than I I can figure things out.
To start with, don't look at the code posted.  Start with replys 18, and 19 (or earlier) and go from there.  Ask as many questions are you need to, to understand what you are doing.  That way you will learn, instead of just copying code.
Title: Re: Get Block
Post by: Kerry on March 16, 2006, 01:44:32 PM
I have removed all my posts from this thread.
Title: Re: Get Block
Post by: nivuahc on March 16, 2006, 01:53:35 PM
I have removed all my posts from this thread.

Why?

Even if they don't help joker, surely they have merit and could possibly help someone else
Title: Re: Get Block
Post by: CAB on March 16, 2006, 11:11:06 PM
Ok, I stared this earlier but the thread took off faster than I could write this
so I held off posting. This is my feeble attempt at explaining how this lisp could
be written. Note that there are many ways to code so I am attempting to take a very
basic approach to this routine. This is what I have so far & lack time to finish so
please someone step in & complete the routine & expiation and correct any mistakes
I have made. Also fill in any gaps I left. <please excuse the spelling & grammar too> :)

joker you will need do create a function you can call from the command line.
It may look like this
Code: [Select]
(defun c:ReBlock()
; your code goes here
)
Note that for every opening parentheses there must be a closing parenthesis.
See the help file for explanation of a defun
You can call ReBlock from the command line, It won't do anything yet but you will fix that.
Also note that the semicolon starts a comment
From the Help File:
Code: [Select]
; Single-Semicolon Starts at the comment-column position, as defined by the
                           "Single-Semicolon comment indentation" format option.
;; Current-Column         The comment appears starting on a new line, indented at the 
                          same level as the last line of program code.
;;; Heading or 0-Column Appears on a new line, without indentation.

So this is what I often do, wrap the pseudo code with the function as Tim did earlier.
He picked ReBlock as the function name but you can pick your own name. Obviously it
should be meaningful to you.
Code: [Select]
(defun c:ReBlock()
  ;User picks block to export
  ;Insert the block at 0,0
  ;Explode the block
  ;Collect the new entities in a list
  ;Change all the entities to color of layer if color ByLayer
  ;Change all the entities to layer zero
  ;wblock out the explode pieces, ? any user input ?
  ;Delete the explode pieces
)
Now you start adding code to preform the tasks described. Note that the pseudo code may
change as you develop your routine & discover things you did not consider in the beginning.
That's just part of the process, for me that is.

Lets add the code that tim provided to explode & collect. He will explain it later.
Let me say that there are some error prevention & recovery code that you will want but
to keep it simple at first just stick to the basic pseudo code requirements first.

Code: [Select]
(defun c:ReBlock()
  ;User picks block to export
  ;Insert the block at 0,0
  ;Explode the block and Collect the new entities in a list
  (setq EntList
             (mapcar 'vlax-vla-object->ename
                     (vlax-invoke (vlax-ename->vla-object Ent) 'Explode)
              )
  )
  ;Change all the entities to color of layer if color ByLayer
  ;Change all the entities to layer zero
  ;wblock out the explode pieces, ? any user input ?
  ;Delete the explode pieces
)

OK, how about the first item, picking the block to work with.
Do you see how that may be accomplished?

Looking at Tim's full version we can barrow this:
Code: [Select]
(setq ss (ssget ":E:S" '((0 . "INSERT"))))That's different in that I added ":E:S" which limits the selection to one object.
This code gives control to the user to select object(s) & adds there Entity Name to a 'selection set'
This selection set is stored in the variable named ss. In our case here it will only contain one
item. In Tim's code it can contain many items. (setq ss (ssget '((0 . "INSERT"))))
The '((0 . "INSERT")) is a filter making sure that only INSERT's can be selected. That is the label
given to blocks that exist in model or paperspace.
So here is the updated code:

Code: [Select]
(defun c:ReBlock()
  ;User picks block to export
  (setq ss (ssget ":E:S" '((0 . "INSERT"))))
  ;Insert the block at 0,0
  ;Explode the block and Collect the new entities in a list
  (setq EntList
             (mapcar 'vlax-vla-object->ename
                     (vlax-invoke (vlax-ename->vla-object Ent) 'Explode)
              )
  )
  ;Change all the entities to color of layer if color ByLayer
  ;Change all the entities to layer zero
  ;wblock out the explode pieces, ? any user input ?
  ;Delete the explode pieces
)

Now we need the "Entity Name" of that insert. We get that with this:
Code: [Select]
(setq ename (ssname ss 0))So ename is the variable now holding the item entity name the user selected.
To get the block name we look at DXF code 2 like this:
Code: [Select]
(setq blkname (cdr (assoc 2 (entget ename))))WE can use it to insert a copy of that block by using the INSERT command.
Code: [Select]
(command "_.INSERT" blkname '(0.0 0.0 0.0) 1 1 1 0)
Here is the code so far.
Code: [Select]
(defun c:ReBlock()
  ;User picks block to export
  (setq ss (ssget ":E:S" '((0 . "INSERT"))))
  ;Get the block name from the entity list
  (setq ename (ssname ss 0))
  (setq blkname (cdr (assoc 2 (entget ename))))
  ;Insert the block at 0,0 and save its ename in variable ent
  (command "_.INSERT" blkname '(0.0 0.0 0.0) 1 1 1 0)
  (setq ent (entlast))
  ;Explode the block and Collect the new entities in a list
  (setq EntList
             (mapcar 'vlax-vla-object->ename
                     (vlax-invoke (vlax-ename->vla-object Ent) 'Explode)
              )
  )
  ;Change all the entities to color of layer if color ByLayer
  ;Change all the entities to layer zero
  ;wblock out the explode pieces, ? any user input ?
  ;Delete the explode pieces
)
Title: Re: Get Block
Post by: Jim Yadon on March 16, 2006, 11:44:39 PM
You guys are moving faster than I I can figure things out.

I know how you feel. Sometimes I feel like the family dog on National Lampoon's Vacation (http://www.imdb.com/title/tt0085995/). It's really great when the guys get going. It's just unfortunate that they forget sometimes that they've got us tied to the bumper with our lack of knowledge. LISP is not one of my strong points.
Title: Re: Get Block
Post by: Kerry on March 16, 2006, 11:58:39 PM
Joker,

Where are you from and what is your native language ?
How well do you understand English ?

Have you done any customisation of AutoCAD at all ? if so, what ?

.. knowing this will make people better able to assist you.

Title: Re: Get Block
Post by: MP on March 17, 2006, 07:27:21 AM
I have removed all my posts from this thread.

(http://www.theswamp.org/screens/mp/eh.png)

Title: Re: Get Block
Post by: M-dub on March 17, 2006, 08:09:45 AM
I have removed all my posts from this thread.

(http://www.theswamp.org/screens/mp/eh.png)



Took the words right out of my keyboard!
Title: Re: Get Block
Post by: LE on March 17, 2006, 10:48:40 AM

Where are you from and what is your native language ?
How well do you understand English ?

Have you done any customisation of AutoCAD at all ? if so, what ?

.. knowing this will make people better able to assist you.


Can the above be part of a FAQ, as many times someone not having English as a native language [like me], might translated or interpreted words literally... Things like Jokes, double meaning words, or bad words for example [that are used here]... are hard to understand...
Title: Re: Get Block
Post by: nivuahc on March 17, 2006, 11:04:05 AM
Can the above be part of a FAQ, as many times someone not having English as a native language [like me], might translated or interpreted words literally... Things like Jokes, double meaning words, or bad words for example [that are used here]... are hard to understand...

That's actually a pretty good suggestion, LE. A daunting task, to say the least, but good idea nonetheless.
Title: Re: Get Block
Post by: T.Willey on March 17, 2006, 11:57:56 AM
Joker,

  Once you get as far as Alan (CAB) has shown, let us know, and we will show you have to finish the code.
Title: Re: Get Block
Post by: Jim Yadon on March 17, 2006, 09:35:05 PM
... or bad words for example [that are used here]... are hard to understand...

Someone used foul language here? :?
Title: Re: Get Block
Post by: Slim© on March 17, 2006, 09:59:44 PM
Ducks, chickens, geese, quail....
(http://www.theswamp.org/lilly_pond/index.php?dir=slim/&file=aga_oiseaujauned.gif)     (http://www.theswamp.org/lilly_pond/index.php?dir=slim/&file=aga_oiseaujauneg.gif)
Title: Re: Get Block
Post by: Kerry on March 18, 2006, 12:42:20 AM
actually,
Quote
.. take most comments with a grain of salt.
is pretty foul, particularly to anyone who labours to be accurate, objective and impartial.