TheSwamp

Code Red => VB(A) => Topic started by: daron on March 17, 2005, 09:18:51 AM

Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 09:18:51 AM
I am using a program in autocad that the original creator not only ecrypted the file but didn't collect any variables to return to their original state. What I'm in need of is an event module that collects the current layer when a lisp is called and restores that layer when the lisp is ended or cancelled. Will someone please throw out a bone with some meat on it. Thanks.
Title: In need of a handout. Please.
Post by: Kerry on March 17, 2005, 09:24:11 AM
Could you wrap it ?

defun new-command
 
trap variables

call old-defunct-crappy0command

restore vars'
Title: In need of a handout. Please.
Post by: CmdrDuh on March 17, 2005, 10:12:28 AM
can you un-encrypt it?, also, what does it do, can we replicate it fairly easily
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 10:48:53 AM
I have gotten it unencrypted. There are a bunch of functions without a c: prompt and a bunch of code without a defun. It's a HUGE file. My problem is I'm not sure about encryption. How does autocad differentiate between an encrypted file and an unencrypted file or does that matter? I'm in process of creating a whole new program for this job, but it's a slow process. I just was hoping for a bandage for what I'm currently using.

Kerry, what you mention about the wrapping it? Isn't that what beginlisp and endlisp / cancellisp do? It's the beginLisp event that I'm not sure how to write. It'd be nice if the event could be fired anytime any lisp is called.
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 11:05:56 AM
I don't mean to be a snot, but decrypting a program? Generally speaking this is a no no even though it's easy to do. Can you get the original source code from the author?
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 11:16:48 AM
I don't plan to update or even take from the original code. I just want something to keep it from setting me to layer 0 when I think I'm on a different layer.
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 11:20:04 AM
Am I oversimplifying it by suggesting (command ".layerp")?
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 11:21:19 AM
Where? Yes. I don't care what the previous layer was, I just want to not be left on a layer I wasn't on. Why can't people who charge for code write code that plays nice?
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 11:21:46 AM
After?
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 11:24:32 AM
Was in process of editing last post as you posted. See previous post.
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 11:26:11 AM
Charging for code does not a professional make?
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 11:39:25 AM
Very true. Can a vba wrapper be created for this problem?
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 11:48:47 AM
I was under the impression it was a lisp program?

Perhaps something like ...

Code: [Select]
(defun c:Wrap ( / vars )

    (setq vars
        (mapcar
           '(lambda (varname) (cons varname (getvar varname)))
           '("cmdecho" "clayer" "cecolor" "celtype")
        )        
    )
   
    (c:CallTheOffendingProgram)
   
    (foreach pair vars
        (vl-catch-all-apply
           '(lambda ()
                (setvar (car pair) (cdr pair))
            )
        )    
    )
   
    (princ)

)
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 12:09:54 PM
If the code runs on load you could do something like this ...

Code: [Select]
(defun c:Wrap ( / vars )

    (setq vars
        (mapcar
           '(lambda (varname) (cons varname (getvar varname)))
           '("cmdecho" "clayer" "cecolor" "celtype")
        )        
    )
   
    (load "TheOffendingProgram" (princ "The load failed."))
   
    (foreach pair vars
        (vl-catch-all-apply
           '(lambda ()
                (setvar (car pair) (cdr pair))
            )
        )    
    )
   
    (princ)

)
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 12:12:41 PM
It is a lisp program that is the offender, but I was under the impression that vba could handle events better than lisp. Would I have to know each lisp name to watch for? Nice code by the way.
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 12:15:50 PM
Didn't see that second one. I'll give that a try, because you are partly correct. To get the lisp loaded, they gave a toolbar that has to be pressed each time you intend to work with this program. Typing wrap, might work.
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 12:22:00 PM
You could do it via event code but I generally avoid event based code unless it's absolutely essential. I don't see this as sporting such necessity.

But I don't pretend to fully appreciate your situation, I'm reading / posting rather quickly here (juggling writing two programs as we speak).

Thanks for your compliment. If I had more time I'd wrapper the wrapper as a generic function you merely pass an argument, like:

Code: [Select]
(defun safecall ( quotedStatement )

    ;; save vars per previous post

    ;; vl-catch-all-apply the quoted statement

    ;; safely restore vars per previous post

    ;; shhhh

)
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 12:35:58 PM
The code is nice, but the lisp problem remains, because each successive button I have to use ruins my settings the same way. There are a lot of buttons. I do feel a neccesity for event handling. I am aware of the adverse effects.

Here's one I put together for an osmode problem, but the clayer problem needs to know what to set it to, previous to the event.
Code: [Select]
Private Sub AcadDocument_EndLisp()
    Dim sysVarName As String
    Dim sysVarData As Variant
    Dim DataType As Integer
   
    Dim intData As Integer
    sysVarName = "osmode"
    intData = 6175
    sysVarData = intData
    ThisDrawing.SetVariable sysVarName, sysVarData
End Sub

Private Sub AcadDocument_LispCancelled()
    Dim sysVarName As String
    Dim sysVarData As Variant
    Dim DataType As Integer
   
    Dim intData As Integer
    sysVarName = "osmode"
    intData = 6175
    sysVarData = intData
    ThisDrawing.SetVariable sysVarName, sysVarData
End Sub


It's the BeginLisp that I don't quite have a handle on.
Title: In need of a handout. Please.
Post by: MP on March 17, 2005, 12:39:17 PM
Let me apoligize in advance - no time to post / write more code - but let me say be very careful how you implement this, lest it affect every lisp program.
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 12:41:15 PM
Of course. This work isn't for anybody but myself to cure a headache until I can develop a better solution to do my job.
Title: In need of a handout. Please.
Post by: Keith™ on March 17, 2005, 02:56:00 PM
Daron, use a "BeginLisp" event to grab the current layer and EndLisp event to reset the current layer.
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 03:00:23 PM
I got the EndLisp part. It's the BeginLisp that I can't seem to wade through. Will you show me how to get through that puddle?
Title: In need of a handout. Please.
Post by: Keith™ on March 17, 2005, 03:23:09 PM
Code: [Select]

Option Explicit
Dim CurrLayer As AcadLayer 'global layer variable

Private Sub AcadDocument_BeginLisp(ByVal FirstLine As String)
 If CurrLayer Is Nothing Then 'if there is nothing in the variable now
'this solves the issue of nested lisp functions (not sure if they fire an event)
' but better safe than sorry
  Set CurrLayer = ThisDrawing.ActiveLayer 'save something there
 End If
End Sub

Private Sub AcadDocument_EndLisp()
 If CurrLayer Is Nothing Then ' if there is nothing in the variable
 ' we do nothing (unusual caveat).... it is not easy to test if a value is
' FALSE when comparing to NOTHING .. VBA generates an error ..
 Else
  ThisDrawing.ActiveLayer = CurrLayer 'Set the layer current
  Set CurrLayer = Nothing 'clear the variable
 End If
End Sub

Private Sub AcadDocument_LispCancelled()
 If CurrLayer Is Nothing Then' if there is nothing in the variable
 ' we do nothing (unusual caveat).... it is not easy to test if a value is
' FALSE when comparing to NOTHING .. VBA generates an error ..
 Else
  ThisDrawing.ActiveLayer = CurrLayer 'Set the layer current
  Set CurrLayer = Nothing 'clear the variable
 End If
End Sub


Ok, note that I used a global variable that I can set that will be active across several events. Now, please note that this WILL cause an error if you change the current drawing while in the middle of a lisp program. You could change the global variable to hold the drawing name and layer, then check for the drawing name and layer of the "current" drawing... but that was just a little more coding than I have time for tright now ...
Title: In need of a handout. Please.
Post by: daron on March 17, 2005, 04:21:39 PM
Thank you Keith. I appreciate it.
Title: In need of a handout. Please.
Post by: Kerry on March 18, 2005, 04:43:19 AM
Quote from: Daron
<<< Kerry, what you mention about the wrapping it? Isn't that what beginlisp and endlisp /  <<<<.


 Sorry, I've been away most of the day .. I meant wrapped like Michaels post 13 an 14 show.
Title: In need of a handout. Please.
Post by: daron on March 18, 2005, 07:47:38 AM
Thanks everybody. Keith, the beginlisp etc works beautifully. The help file makes it seem more complicated than that.