See the top rated post in this thread. Click here

Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Evaluating string as vb.net expression

  1. #11
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Evaluating string as vb.net expression

    Group,

    A kludge is just one solution.

    A work around.

    To me programming is not always straight forward, and there are brick walls that need work arounds.

    Usually I try to find all of the work arounds as I can.

    I rarely use code from other programmers as is.

    But I learn best from your examples.

    I would first have to figure out exactly how the function works, what all of the arguments, functions, what each expression does, return values and their limitations.

    Then I would re-write the code in a syntax and order that makes sense to me, using my preferred variable naming convention and so I understand every single line.

    Then I would continue searching for more options, and finally make the selection as to the best method or work around.

    Sometimes a kludge could be the best solution, but without doing the research and studying the code I wouldn't use them either.

    Also there is a balance between runtime speed and development time. Depending on how often a routine is used dictates the development environment and language to be used. LISP is really quick to develop, (especially for applications that only function inside AutoCAD) but very slow at runtime, .NET is slower to develop, for simple applications, but much faster at runtime.

    I agree the development time is relative to experience with the language and the development environment and existing libraries you have developed with the language.

    It would be cool to have an AutoCAD programmers olympics and develop races to see what is the fastest language/development environment to write, debug and implement an application from scratch.

    Peter
    Last edited by peter; 2010-05-27 at 03:14 PM.
    AutomateCAD

  2. #12
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    1

    Default Re: Evaluating string as vb.net expression

    YES! That's exactly what I was trying to say!!! "kludges are cool"!!!

    Talk about missing the point. I apologize if the majority of the posts here are beneath some people but we're trying to learn this stuff and there's not much out there that's worth the hours of seaching on the web. We do find it here though. We probably all migrated from Lisp and there's still a place for it in programming. Most of us just want to draw our lines faster and smarter or insert layers geared to our situation a little easier. A lot of us just want to say "look at what I can make AutoCAD do!".

    Thanks for slapping the kid in me off my bike just because I use training wheels.

  3. #13
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Evaluating string as vb.net expression

    I agree that Kludges are cool also.

    Be careful though. don't use it until you understand it.

    Also I have heard of very few horror storries about some irreversable damage done to a database of drawing library from LISP. (Tony as a matter of fact that was the first one.) For the at least tens of thousands of hack lisp programmers/operators who have been working for years, I suppose there unboubtably will be a rare issue like that.

    For the most part LISP Programmers are Autocad users.

    Drafting on AutoCAD many times can follow two work flows. One is repeat processes manually to accomplish your work, or create programs to automate the work flow.

    Every time I approach a detailing, engineering or analysis task, I evaluate the time to code the solution to automate the process versus the time to do it manually.

    Obviously I frequently use the automation work flow method. So do all of the other LISP programmers.

    It is everything about the time to create the application.

    Most drafting operations are small (less than 10 people) the customization is usually done in house by the people who do the work. They are underpaid, undertrained and underfunded.

    I just finished totally re-writing software written by professional programmers (for a product line we aquired) who were not AutoCAD users that was totally in-efficient for detailing. It placed most of the data entry into dialog boxes, as compared to being selected on screen etc...

    Also personally I have very rarely found a limitation of LISP that required alternate programming languages.

    (Just for fun, a couple weeks ago I taught AutoCAD to talk with two pages of lisp code.)

    I guess we should agree to disagree on the Value of LISP programming. I personally have a lot of respect for the AutoCAD User / lisp programmer. Only he truely knows his work flow.

    I agree the one routine that I posted on the swamp was vb6, but I am not finished yet.

    Before you criticize my investigation, why don't you wait to see what I come up with.

    I personally think there is plenty of room for arx programmers developing software for AutoCAD, but I think the bulk of the applications can be developed in the flavor of choice either LISP or .NET.

    Let me just say that we both have extensive differing experience and are looking at these issues from different perspectives.

    Lets just agree that from our individual perspectives we have differing opinions regarding the value and importance for the programming options for AutoCAD.

    Peter
    AutomateCAD

  4. #14
    I could stop if I wanted to hugh.69031's Avatar
    Join Date
    2016-01
    Location
    Melbourne, Australia
    Posts
    360
    Login to Give a bone
    0

    Default Re: Evaluating string as vb.net expression

    .NET doesn't provide an eval, but you can invoke vbscript:
    http://www.devx.com/vb2themax/Tip/18773
    http://weblogs.asp.net/rosherove/art...scripting.aspx

    So following the example shown here
    http://through-the-interface.typepad...g_started.html

    In Visual Studio VB.NET:
    • create a project called VBMgdAcad1
    • add a reference to the COM Microsoft Script Control
    • then compile this VB.NET code :

    Imports Autodesk.AutoCAD.Runtime


    PublicClass AAAAClass
    ' Define command 'Asdkcmd1'
    <CommandMethod("Asdkcmd1")> _
    PublicSub Asdkcmd1()
    MsgBox("Hello!" + myEval("12 + 3 * 10"))
    EndSub
    PublicFunction myEval(ByVal expr AsString) AsString
    Dim sc AsNew MSScriptControl.ScriptControl()
    sc.Language = "VBScript"
    myEval = sc.Eval(expr)
    EndFunction
    EndClass



    In ACAD:
    • NETLOAD VBMgdAcad1.dll
    • Issue Asdkcmd1 at ACAD's command prompt
    to see Hello!42 in a messagebox.


    hth
    Hugh Adamson
    www.hatchkit.com.au
    Last edited by hugh.69031; 2010-05-29 at 06:25 AM.

  5. #15
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Evaluating string as vb.net expression

    I got this one to work

    (EvaluateVB "MsgBox (\"Hello World\")")

    It does create a unknown assembly so will cause a memory leak if used excessively.

    But it works for a compile at runtime function. You can also import a file.vb file too if you play with a couple methods.

    A certain expert I corresponded with regarding this function said.

    "Compiler as a Service is going to be a key feature in .NET 5"



    so something like this will be available in a couple years without the baggage.

    I thought that was good news.

    Peter

    It is a modified form of the original code on eggheadcafe that was recommended by Bobby C. Jones over on the swamp. See link.

    http://www.eggheadcafe.com/articles/20030908.asp



    Code:
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices 
    Imports Autodesk.AutoCAD.EditorInput 
    Imports Autodesk.AutoCAD.Geometry 
    Imports Autodesk.AutoCAD.GraphicsInterface
    Imports Autodesk.AutoCAD.Runtime
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Text
    Imports System.CodeDom.Compiler
    Imports System.Reflection
    Imports System.IO
    
    
    
    Public Class EvaluatorVBClass
    
    ' Syntax (EvaluateVB "MsgBox (\"Hello World\")")
    
        <LispFunction("EvaluateVB")> _
        Public Function EvaluateVB(ByVal rbfLISPArguments As ResultBuffer) As TypedValue
            Dim arrLISPArguments As TypedValue() = rbfLISPArguments.AsArray
            Dim objReturn As Object = Evaluate(arrLISPArguments(0).Value.ToString)
            If Not objReturn = Nothing Then
                Select Case (objReturn.GetType.ToString)
                    Case "System.Int16"
                        Return New TypedValue(5003, objReturn)
                    Case "System.Int32"
                        Return New TypedValue(5010, objReturn)
                    Case "System.String"
                        Return New TypedValue(5005, objReturn)
                    Case "System.Double"
                        Return New TypedValue(5001, objReturn)
                End Select
            End If
    
            Return New TypedValue(5019, -1)
    
        End Function
        Public Function Evaluate(ByVal vbCode As String) As Object
            'Dim objVBCodeProvider As VBCodeProvider = New VBCodeProvider
    
    
            Dim objCodeDomProvider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
            Dim objCompilerParameters As CompilerParameters = New CompilerParameters()
    
            objCompilerParameters.GenerateInMemory = True
            ' objCompilerParameters.OutputAssembly = "C:\Documents and Settings\peter.CORDECKSALES\My Documents\Visual Studio 2008\Projects\Evaluation\EvaluateVB\EvaluateVB\bin\Debug\testeval.dll"
    
            objCompilerParameters.ReferencedAssemblies.Add("system.dll")
            'objCompilerParameters.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll")
    
            objCompilerParameters.CompilerOptions = "/t:library"
    
            Dim objStringBuilder As StringBuilder = New StringBuilder("")
    
            objStringBuilder.Append("Imports System" & vbCrLf)
            objStringBuilder.Append("Imports Microsoft.VisualBasic" & vbCrLf)
    
            objStringBuilder.Append("Namespace Code  " & vbCrLf)
            objStringBuilder.Append("Class Library " & vbCrLf)
            objStringBuilder.Append("public function  EvalCode() as Object " & vbCrLf)
            'objStringBuilder.Append("Dim objResult as Object " & vbCrLf)
            'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()")
            objStringBuilder.Append("Dim objResult as Object = " & vbCode & vbCrLf)
    
            objStringBuilder.Append("Return objResult" & vbCrLf)
            objStringBuilder.Append("End Function " & vbCrLf)
            objStringBuilder.Append("End Class " & vbCrLf)
            objStringBuilder.Append("End Namespace" & vbCrLf)
    
            Dim objCompilerResults As CompilerResults
            '
            objCompilerResults = _
                objCodeDomProvider.CompileAssemblyFromSource(objCompilerParameters, objStringBuilder.ToString())
    
    
            If objCompilerResults.Errors.Count > 0 Then
                ' Display compilation errors.
                Dim objCompilerError As CompilerError
                For Each objCompilerError In objCompilerResults.Errors
                    MsgBox(objCompilerError.ToString())
                Next objCompilerError
            Else
                Dim objAssembly As System.Reflection.Assembly = objCompilerResults.CompiledAssembly
                Dim objInstance As Object = objAssembly.CreateInstance("Code.Library")
                Dim objType As Type = objInstance.GetType()
                Dim objMethodInfo As MethodInfo = objType.GetMethod("EvalCode")
                Return objMethodInfo.Invoke(objInstance, Nothing)
            End If
            Return Nothing
        End Function
    
    End Class
    AutomateCAD

Page 2 of 2 FirstFirst 12

Similar Threads

  1. how to replace only a single string by pre decided string.
    By mohammad.raees637333 in forum AutoLISP
    Replies: 3
    Last Post: 2014-04-22, 01:07 PM
  2. CM22-1: Evaluating and Training Your Users
    By Autodesk University in forum CAD Management
    Replies: 0
    Last Post: 2012-11-24, 07:38 PM
  3. Testplan for evaluating plotters
    By campbell.58097 in forum Hardware
    Replies: 0
    Last Post: 2012-04-19, 01:45 PM
  4. Evaluating ACE
    By Ed Jobe in forum AutoCAD Electrical - General
    Replies: 1
    Last Post: 2006-09-11, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •