Author Topic: ResultBuffers and LispFunction()  (Read 19405 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #15 on: April 07, 2007, 03:52:07 AM »
These are the relationships :
Code: [Select]
/*
  LispDataType.Angle        // RTANG        = 5004
  LispDataType.DottedPair   // RTDOTE       = 5018
  LispDataType.Double       // RTREAL       = 5001
  LispDataType.Int16        // RTSHORT      = 5003
  LispDataType.Int32        // RTLONG       = 5010
  LispDataType.ListBegin    // RTLB         = 5016
  LispDataType.ListEnd      // RTLE         = 5017
  LispDataType.Nil          // RTNIL        = 5019
  LispDataType.None         // RTNONE       = 5000
  LispDataType.ObjectId     // RTENAME      = 5006
  LispDataType.Orientation  // RTORINT      = 5008
  LispDataType.Point2d      // RTPOINT      = 5002
  LispDataType.Point3d      // RT3DPOINT    = 5009
  LispDataType.SelectionSet // RTPICKS      = 5007
  LispDataType.T_atom       // RTT          = 5021
  LispDataType.Text         // RTSTR        = 5005
  LispDataType.Void         // RTVOID       = 5014
  */
  //const int RTDXF0        = 5020; /* DXF code 0 for ads_buildlist only */
  //const int RTRESBUF      = 5023; /* resbuf */
  //const int RTMODELESS    = 5027; /* interrupted by modeless dialog */

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #16 on: April 07, 2007, 04:20:15 AM »
a little hack to test ...
The enumerated


Code: [Select]
        [LispFunction("ReturnToMe")]
        static public ResultBuffer ReturnToMe(ResultBuffer args)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            foreach (TypedValue rbtv in args)
            {
                switch (rbtv.TypeCode)
                {
                    case (Int32)LispDataType.ListBegin:
                        ed.WriteMessage("(");
                        break;
                    case (Int32)LispDataType.ListEnd:
                        ed.WriteMessage(")");
                        break;
                    case (Int32)LispDataType.Text:
                        ed.WriteMessage("\"{0}\"", rbtv.Value);
                        break;
                    case (Int32)LispDataType.T_atom:
                        ed.WriteMessage(" T ");
                        break;
                    case (Int32)LispDataType.Nil:
                        ed.WriteMessage(" nil ");
                        break;
                   
                    default:
                        ed.WriteMessage(" {0} ", rbtv.Value);
                        break;
                }
            }
            return args;
        }
        //


Quote
(ReturnToMe "A" 1 2.3 '(4 . 5) '(6 7 8 ) (= 1 1.0))
;-> ("A" 1 2.3 (4 . 5) (6.0 7.0 8.0 ) T)

(setq testval "Message")
(ReturnToMe (/ 7 8.0)
               (CONS "one" 1)
               (STRCAT testval " goes here.")
               (GETPOINT "Pick a point")
               (ENTSEL)
)

;-> (  0.875
   ("one" . 1)
   "Message goes here."
   (-200.0 5639.93 0.0)
   (<Entity name: 7efb52f0> (-204.739 2994.1 0.0))
)

The obligatory piccy:
« Last Edit: April 07, 2007, 06:38:51 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #17 on: April 07, 2007, 04:29:54 AM »
Just an observation :

the case test in this :
Code: [Select]
                    case (Int32)LispDataType.ListBegin:
                        ed.WriteMessage("(");
                        break;
may seem excessive, but the compiler converts it to it's numeric value , so it's not process intensive
ie
Code: [Select]
            case 0x1398:
                editor.WriteMessage("(");
                break;
and the IDE IntelliSense helps with the syntax :
The obligatory piccy:
« Last Edit: April 07, 2007, 06:38:33 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #18 on: April 07, 2007, 12:02:24 PM »
Kerry;

I was talking about on how to build the output list for autolisp, anyway I found it, here is (it is the same criteria when using arx):

Code: [Select]
[LispFunction("SummationR")]
public object summationr(ResultBuffer args)
{
    ResultBuffer res = new ResultBuffer();
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Dictionary<string, short> openWith = new Dictionary<string, short>();
    IEnumerator it = args.GetEnumerator();
    while (it.MoveNext())
    {
        short ad = 0, num = 0;
        TypedValue val = (TypedValue)it.Current;
        if ((LispDataType)val.TypeCode != LispDataType.ListBegin && (LispDataType)val.TypeCode != LispDataType.ListEnd)
        {
            string sKey = val.Value.ToString();
            it.MoveNext();
            val = (TypedValue)it.Current;
            num = (short)val.Value;
            try
            {
                openWith.Add(sKey, num);
            }
            catch (ArgumentException)
            {
                if (openWith.TryGetValue(sKey, out ad))
                {
                    num = (short)(num + ad);
                    openWith.Remove(sKey);
                    openWith.Add(sKey, num);
                }
            }
        }
    }
    res.Add(new TypedValue((int)LispDataType.ListBegin, -1)); // open the main list "("
    foreach (KeyValuePair<string, short> kvp in openWith)
    {
        res.Add(new TypedValue((int)LispDataType.ListBegin, -1)); // open a list "("
        res.Add(new TypedValue((short)LispDataType.Text, kvp.Key)); // add the key "str"
        res.Add(new TypedValue((short)LispDataType.Int16, kvp.Value)); // add the value 'short'
        res.Add(new TypedValue((int)LispDataType.ListEnd, -1)); // close the list ")"
    }
    res.Add(new TypedValue((int)LispDataType.ListEnd, -1)); // close the main list ")"
    return res;
}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #19 on: April 07, 2007, 06:50:30 PM »
Luis,
Why are you casting the LispDataType enumerator to int in some cases and to short in others ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #20 on: April 07, 2007, 07:08:57 PM »
Luis,
Why are you casting the LispDataType enumerator to int in some cases and to short in others ?

If I not, I get some errors, that's was the only way to make it work.

I think I understood your question, it is a habit... it can be all (int)'s


Isn't it a pity,
George
« Last Edit: April 07, 2007, 07:20:20 PM by George Harrison »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #21 on: April 07, 2007, 07:23:17 PM »
Are these the Namespaces you are using ?
 
Code: [Select]
           System.Collections.Generic.Dictionary<string, short>
                openWith = new Dictionary<string, short>();
            System.Collections.IEnumerator
                it = args.GetEnumerator();
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #22 on: April 07, 2007, 07:25:54 PM »
.. but why  int in some cases and to short in others ?

It's was my habit I checked and can be all the casts as (int)

Quote
did this line work for you ?
 IEnumerator it = args.GetEnumerator();

Yes.

Quote
using System;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcWd = Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.LayerManager;
using System.Collections;
using AddTicks17;

[assembly: CommandClass(typeof(ClassLibrary.LESQClass))]
...


Isn't it a pity,
George

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #23 on: April 07, 2007, 07:42:10 PM »
(("one" 5) ("two" 9))


what's with the "Isn't it a pity" ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #24 on: April 07, 2007, 07:46:21 PM »
what's with the "Isn't it a pity" ?

Hey... that's the name of one of my songs  :laugh:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #25 on: April 07, 2007, 08:49:58 PM »
For the spectators ..

The TypedValue structure holds 2 private properties
m_typeCode which is a short .. [Int16]
m_value which is an object

one of the constructors for the TypedValue struct is built like this :
Code: [Select]
public TypedValue(int typeCode, object value)
{
    this.m_typeCode = (short) typeCode;
    this.m_value = value;
}
... which accepts arguments of type  int [Int32] and object respectively.

The public accessor  to the internal m_typeCode returns a short [Int16]

The compiler will allow implicit type conversion from lower to higher,
so passing a short where an int is expected is generally accepted.

.. so, while the constructor is defined as:
(new TypedValue((int)LispDataType.Text, kvp.Key))
the comliler will allow
(new TypedValue((short)LispDataType.Text, kvp.Key))


BTW.
The (int) in front of (int)LispDataType... is called a cast. It is casting (converting) the type to int in this case, ensuring type integrity.

also BTW.
The  LispDataType.Text enumerator always evaluates to a numeric value of 5005
so we could have used
(new TypedValue(5005, kvp.Key))


Int16 has a MaxValue = 32767
Int32 has a MaxValue = 2147483647




« Last Edit: April 07, 2007, 08:56:04 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #26 on: April 07, 2007, 09:22:32 PM »
Kerry;

My idea/intention was just to use LispDataType as was mentioned here.


 :-)
Beware of darkness
« Last Edit: April 09, 2007, 09:56:16 AM by le »

TonyT

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #27 on: April 07, 2007, 11:23:37 PM »
You can use constants or LispDataType as each makes
the code more explicit than using 0xNNNN....

Code: [Select]

           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            foreach (TypedValue rbtv in args)
            {
                switch (rbtv.TypeCode)
                {
                    case (Int32)LispDataType.ListBegin:
                        ed.WriteMessage("(");
                        break;
                    case (Int32)LispDataType.ListEnd:
                        ed.WriteMessage(")");
                        break;

                       ........

could have also been done thusly:

           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            foreach (TypedValue rbtv in args)
            {
                switch ((LispDataType) rbtv.TypeCode)    /////////<- just cast this
                {
                    case LispDataType.ListBegin:        ///// rather than each of these
                        ed.WriteMessage("(");
                        break;
                    case LispDataType.ListEnd:
                        ed.WriteMessage(")");
                        break;



Just an observation :

the case test in this :
Code: [Select]
                    case (Int32)LispDataType.ListBegin:
                        ed.WriteMessage("(");
                        break;
may seem excessive, but the compiler converts it to it's numeric value , so it's not process intensive
ie
Code: [Select]
            case 0x1398:
                editor.WriteMessage("(");
                break;
and the IDE IntelliSense helps with the syntax :
The obligatory piccy:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #28 on: April 07, 2007, 11:49:58 PM »
                       ........
// could have also been done thusly:
                 switch ((LispDataType) rbtv.TypeCode)    /////////<- just cast this

Yep, I'd made those changes this morning .. fresh light and fresh coffee .. ;-)

Thanks Tony.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #29 on: April 07, 2007, 11:54:29 PM »
I introduce my first LispFuction.

Code: [Select]
........
 [LispFuction("PolarPoint")]
 public ResultBuffer DotNetfunction(ResultBuffer rbfArgs)
 {
 ..............}

gilseorin,
how about something like this ?
Code: [Select]
        // (PolarPoint '(0.0 0.0 0.0) 0.2617993877 1000.0)
        [LispFunction("PolarPoint")]
        public ResultBuffer DotNetPolarPoint(ResultBuffer rbfArgs)
        {
            TypedValue[] arInputArgs = rbfArgs.AsArray();
            Point3d co = (Point3d)((TypedValue)(arInputArgs.GetValue(0))).Value;
            double ang = (double)((TypedValue)(arInputArgs.GetValue(1))).Value;
            double dst = (double)((TypedValue)(arInputArgs.GetValue(2))).Value;

            ResultBuffer rbfResult =
                new ResultBuffer(
                    new TypedValue((int)LispDataType.Point3d,               
                        new Point3d(co.X + dst * System.Math.Cos(ang),
                                    co.Y + dst * System.Math.Sin(ang),
                                    co.Z)));
            return rbfResult;
        }

added:duh!
co.Z was  co.Y
made it a bit too upendicular :-)
« Last Edit: April 08, 2007, 12:08:56 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.