Author Topic: Please help me with my code  (Read 3531 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Guest
Please help me with my code
« on: March 09, 2011, 10:32:00 AM »
Hi all,

I usually post over at CadTutor but the site is down for repair.  I try not to jump back and forth between forums because I am not sure how you guru's look upon this.

This being said, I could really use some assistance.

All I want to do is check my acad file to see if a PlotTab is there and if it is, delete it and move on.  I have come close but the more I try to figure it out the more screwed up I become and the more confused I become.  This should be simple but I am mind-boggeled.


Code: [Select]
(defun C:test ( / deleteLayout)

  (vl-load-com)

  (setq
    acadObject (vlax-get-acad-object)
    acadActiveDocument (vla-get-ActiveDocument acadObject)
    acadLayouts (vla-get-Layouts acadActiveDocument)
  )

;-----------------------------------------------------------------------------------------

    (vla-StartUndoMark acadActiveDocument) ;Start of UNDO

;-----------------------------------------------------------------------------------------

  (vlax-for eachLayout acadLayouts
    (if
      (eq (vla-get-name eachLayout) Plot)
      (progn
(setq Layout eachLayout)
(vla-delete Layout)
      )
    )
  )



;-----------------------------------------------------------------------------------------

    (vla-EndUndoMark acadActiveDocument) ;End of UNDO

;-----------------------------------------------------------------------------------------

    (princ)
)


Thanks

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Please help me with my code
« Reply #1 on: March 09, 2011, 10:37:08 AM »
The 'Plot' symbol doesn't seem to have a value.

Otherwise I would assume you wanted to compare strings:

Code: [Select]
(eq (vla-get-name <layout>) "Plot")
You might want to also use strcase to avoid case-sensitivity.

BlackBox

  • King Gator
  • Posts: 3770
Re: Please help me with my code
« Reply #2 on: March 09, 2011, 10:37:33 AM »
My two cents, quickly looking over your code:

Code: [Select]
(defun C:test ( / deleteLayout)

  (vl-load-com)

  (setq
    acadObject (vlax-get-acad-object)
    acadActiveDocument (vla-get-ActiveDocument acadObject)
    acadLayouts (vla-get-Layouts acadActiveDocument)
  )

;-----------------------------------------------------------------------------------------

    (vla-StartUndoMark acadActiveDocument) ;Start of UNDO

;-----------------------------------------------------------------------------------------

  (vlax-for eachLayout acadLayouts
    (if
      (eq [color=red](strcase[/color] (vla-get-name eachLayout)[color=red])[/color] [color=red]"PLOT"[/color])
      (progn
(setq Layout eachLayout)
(vla-delete Layout)
      )
    )
  )



;-----------------------------------------------------------------------------------------

    (vla-EndUndoMark acadActiveDocument) ;End of UNDO

;-----------------------------------------------------------------------------------------

    (princ)
)


Thanks
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Please help me with my code
« Reply #3 on: March 09, 2011, 10:38:39 AM »
The 'Plot' symbol doesn't seem to have a value.

Otherwise I would assume you wanted to compare strings:

Code: [Select]
(eq (vla-get-name <layout>) "Plot")
You might want to also use strcase to avoid case-sensitivity.

You beat me to it.  :kewl:
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Please help me with my code
« Reply #4 on: March 09, 2011, 10:49:33 AM »
You beat me to it.  :kewl:

And with your carefully code modifications, he now has a complete answer - good job  8-)

cadman6735

  • Guest
Re: Please help me with my code
« Reply #5 on: March 09, 2011, 12:29:17 PM »
Hey guys :-)

If it isn't the 2 out of the 3 musketeers that always come to my rescue.  (One of these days I will have enough knowledge to repay the education you guys give me to someone else)

Anyway

strcase was the missing piece of my puzzle and this routine works like a charm.  I can move on...  Thanks


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Please help me with my code
« Reply #6 on: March 09, 2011, 12:39:50 PM »
FYI.... from the help, for the Delete method.

Quote
Remarks

When you delete an object in a collection, all remaining items in the collection are reassigned a new index based on the current count. You should therefore avoid loops that delete an object while iterating through the collection.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Please help me with my code
« Reply #7 on: March 09, 2011, 12:42:30 PM »
Good point Tim, I suppose the best way would be to create a list and mapcar 'vla-delete the list.

cadman6735

  • Guest
Re: Please help me with my code
« Reply #8 on: March 09, 2011, 12:49:52 PM »
Tim

I read this too in the help while I was trying to figure out how to delete layout tabs with out using (command "-layout")

But since I am only looking for a layout with the Name "Plot" this didn't seem relevent to my situation...

Should I be rethinking my approach?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Please help me with my code
« Reply #9 on: March 09, 2011, 12:54:13 PM »
I would do it correctly, just so you get in the habit.  Who knows what else might happen later on.

Good point Tim, I suppose the best way would be to create a list and mapcar 'vla-delete the list.

I would use a ' foreach ' and not a mapcar, just because who cares what is returned.  I'm trying to get into that habit.  If I want a list returned, then I use mapcar, but if not, then I use foreach.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

cadman6735

  • Guest
Re: Please help me with my code
« Reply #10 on: March 09, 2011, 12:57:28 PM »
Lee

I was wondering what you ment by this:

Quote
The 'Plot' symbol doesn't seem to have a value.


Took me a while to figure it out but yes, I ment qoute my Plot   "Plot"

As I said I re-wrote this so many time in so many different directions, each getting close to what I wanted but none of them being exact so I forgot to quote my "plot"  This too was a causeing an issue that I did not see.

Thanks

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Please help me with my code
« Reply #11 on: March 09, 2011, 12:57:58 PM »
I would use a ' foreach ' and not a mapcar, just because who cares what is returned.  I'm trying to get into that habit.  If I want a list returned, then I use mapcar, but if not, then I use foreach.

I do find myself using mapcar excessively - sometimes just because it looks more succinct:

Code: [Select]
(foreach layout <layoutlist>
  (vla-delete layout)
)

Code: [Select]
(mapcar 'vla-delete <layoutlist>)

cadman6735

  • Guest
Re: Please help me with my code
« Reply #12 on: March 09, 2011, 01:02:14 PM »
Quote
I would do it correctly, just so you get in the habit.

This is what I want to do, the only problem is I don't know the proper way till someone points it out to me.   :oops:


Quote
Who knows what else might happen later on.

This is what I am trying to avoid, the unknown issues for later, only experiance can provide me this, which I have little of.

I will take your advise and see where it leads me but I am afraid I may not understand till it bites me in the a$$.

Thanks for leading me in a good direction.