See the top rated post in this thread. Click here

Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: .NET Return Conversion to TypedValue

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

    Default .NET Return Conversion to TypedValue

    I was thinking about how to return values to LISP from .NET and played around with some code and came up with this.

    It can be made more complete, but it is a place to start.

    Code:
    Public Function ObjectToTypedValue(ByRef objItem As Object)
            Dim intDatatype As Integer = Nothing
            If objItem Is Nothing Then
                intDatatype = 5000
                objItem = -1
            Else
                Select Case objItem.GetType
                    Case GetType(Double)
                        intDatatype = 5001
                    Case GetType(Point2d)
                        intDatatype = 5002
                    Case GetType(Int16)
                        intDatatype = 5003
                    Case GetType(String)
                        intDatatype = 5005
                    Case GetType(ObjectId)
                        intDatatype = 5006
                    Case GetType(Point3d)
                        intDatatype = 5009
                    Case GetType(Int32)
                        intDatatype = 5010
                    Case GetType(Boolean)
                        If objItem = False Then
                            intDatatype = 5019
                            objItem = -1
                        Else
                            intDatatype = 5021
                            objItem = -1
                        End If
                End Select
            End If
            If Not intDatatype = Nothing Then
                Return New TypedValue(intDatatype, objItem)
            End If
            Return Nothing
        End Function
    Thoughts?
    AutomateCAD

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: .NET Return Conversion to TypedValue

    Quote Originally Posted by peter View Post
    Thoughts?
    This old post should help to simplify:

    Quote Originally Posted by BlackBox View Post
    From post #2 of this thread:

    Re: Return value of a lisp callable function


    Quote Originally Posted by Tony Tanzillo
    A method marked with the LispFunction attribute should return a ResultBuffer only if you are returning a list.

    If you are returning an atom, you just return that value directly.

    For example, this method will return the symbol T:

    Code:
    [LispFunction("foo")]
    public static object Foo( ResultBuffer args )
    {
      return true; 
    }
    and this will return the integer 99:

    Code:
    [LispFunction("bar")]
    public static object Bar( ResultBuffer args )
    {
      return 99; 
    }
    and this will return the string "Hello":

    Code:
    [LispFunction("baz")]
    public static object Baz( ResultBuffer args )
    {
      return "Hello"; 
    }
    IOW, the result of your lisp-callable function does not have to be a ResultBuffer, it can be that, or any value you can put into a TypedValue's Value field as well. The runtime looks at the result and converts it to the appropriate LISP result. In the case of System.Boolean, that would be the symbol T for True, or NIL for False.

    You cannot return COM objects to VisualLisp using LispFunction, but you write a COM-callable wrapper that can return COM objects or any other ActiveX compatible type, which can be called from LISP using VLAX-* functions.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

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

    Default Re: .NET Return Conversion to TypedValue

    The function still does what it is listed to do convert objects to typed values, but as you pointed out it is built in for objects to be converted to typed values when returning to lisp function. The post I made on the multiple file select dialog does return a result buffer for simplicity. It could return an atom with a minor change of code.

    This function above can be used to build result buffers that can be returned as lists.
    AutomateCAD

  4. #4
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: .NET Return Conversion to TypedValue

    Just as an aside, the GetType function isn't very optimal. You might want to rather try using GetTypeHandle instead as the compiler then circumvents getting the entire Type class through reflection. See this: http://blogs.msdn.com/b/vancem/archi...01/779503.aspx

    Another alternative way would be to make a parameter overloaded set of functions. That way the compiler would choose the best suiting one for you, no need for if's and switch's. Not to mention, the functions would be very trivial (usually just the return line). Therefore chances are that the compiler might even inline them, negating the need for passing by reference and not even adding a function stack. Though in DotNet 4.5 you can now force agressive inlining: http://stackoverflow.com/questions/4...functions-in-c

  5. #5
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: .NET Return Conversion to TypedValue

    As an example, here's what I mean (just written it within the last 30 min, since it's mostly copy-paste with minor changes):

    (Edited: See revised code in later post)

    Note I've also added enumerables (i.e. arrays, linked lists, collections, etc.) and also tupples->dotted pairs, not to mention dictionaries to association lists (dotted pairs if value is an atom).

    Edit: Also note I use the built-in constants from LispDataType instead of some "magic-number". The compiler should optimize the lookup and convert to int out of the actual code because they're constants, not variables. This makes the code more readable while not loosing efficiency.
    Last edited by peter; 2014-02-19 at 06:49 PM.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    1

    Default Re: .NET Return Conversion to TypedValue

    The same code in VB.Net:
    Code:
    Public NotInheritable 
    
    (Edited: See revised code in later post)
    Edit: Just noticed something really strange ... copy-pasting from SharpDevelop adds the colour highlights for VB.Net code but not for C#
    Last edited by peter; 2014-02-19 at 06:50 PM.

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

    Default Re: .NET Return Conversion to TypedValue

    Excellent...

    I like the overload method, and it was along the same lines I was envisioning when I coded the function at the top.
    (to include lists, dotted pairs, collections... I would like to see xml too...

    I would hope that as this forum grows, and the simple initial functions are matured (with exception handling etc...) a standard LISP.Interop assembly could be put together that would expose all of these functions to anyone who wants to download them.

    p=
    AutomateCAD

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

    Default Re: .NET Return Conversion to TypedValue

    What is the trick you are using to add the color to the text in the vb.net example?
    AutomateCAD

  9. #9
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: .NET Return Conversion to TypedValue

    Quote Originally Posted by peter View Post
    What is the trick you are using to add the color to the text in the vb.net example?
    That's just it ... no trick ... I was just as surprised when I saw it:
    Quote Originally Posted by irneb View Post
    Edit: Just noticed something really strange ... copy-pasting from SharpDevelop adds the colour highlights for VB.Net code but not for C#
    I've tested it again, it doesn't want to work now! Not even the same VB code. This really is weird!

  10. #10
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: .NET Return Conversion to TypedValue

    Sorry, this is probably totally off topic.

    Just found out it's something to do with this forum. There's definitely something strange going on, I remember that I was pretty fast when pasting from #Dev into the reply-box - the buttons hadn't even loaded yet. Perhaps there's some strange JS code which converts any RTF/HTML text into un-formatted text? I've tried the same from both FF and Chrome, and in both it works fine in other forums, e.g. using this to test: http://www-archive.mozilla.org/editor/midasdemo/

    Seems it's due to the WYSIWYG editor turning itself off all the time: I'll have to remember to click the top-left button in the Advanced Page every time in order to have formatting inside the editor.
    Last edited by irneb; 2014-02-19 at 11:04 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 10
    Last Post: 2016-06-27, 12:04 AM
  2. RETURN TO LDD
    By Wish List System in forum Civil 3D Wish List
    Replies: 13
    Last Post: 2015-10-08, 03:40 PM
  3. Why does this return nil...
    By chris.mackay434411 in forum AutoLISP
    Replies: 7
    Last Post: 2013-11-26, 03:21 PM
  4. Field return Z value WITH conversion factor
    By Kernal_CAD_Monkey in forum AutoCAD Fields
    Replies: 3
    Last Post: 2012-11-09, 03:00 PM
  5. How do you create a Boxed Return or Cornice Return?
    By rgecy in forum Revit Architecture - General
    Replies: 7
    Last Post: 2004-05-20, 04:42 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
  •