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

0 Members and 1 Guest are viewing this topic.

gilseorin

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #30 on: April 08, 2007, 06:02:53 AM »
Sweet and cool!
Thank you for enlarging my knowledge,Kerry.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8696
  • AKA Daniel
Re: ResultBuffers and LispFunction()
« Reply #31 on: April 09, 2007, 09:36:16 AM »
How about using TonyT’s awesome TypedValueList class ?
http://www.theswamp.org/index.php?topic=14495.msg186823#msg186823

Code: [Select]
[LispFunction("PolarPoint")]
    public ResultBuffer DotNetPolarPoint(ResultBuffer rbfArgs)
    {
      //TonyT's cool TypedValueList
      TypedValueList arInputArgs = new TypedValueList(rbfArgs.AsArray());
      TypedValueList arOutputArgs = new TypedValueList();
      //
      Point3d co = (Point3d)arInputArgs[0].Value;
      double ang = (double)arInputArgs[1].Value;
      double dst = (double)arInputArgs[2].Value;
      //
      arOutputArgs.Add((int)LispDataType.Point3d,
                       new Point3d(co.X +
                       (dst * Math.Cos(ang)),
                       co.Y + (dst * Math.Sin(ang)),
                       co.Z));

      return arOutputArgs; //edit
    }

This seems to be the way to go, thanks Tony
« Last Edit: April 09, 2007, 11:20:57 AM by Danielm103 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ResultBuffers and LispFunction()
« Reply #32 on: April 09, 2007, 09:43:16 AM »
I KNEW I'd seen something like that somewhere ... and couldn't remeber where ... and it was in our own garden all the time .. thanks Dan and 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.

gilseorin

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #33 on: April 09, 2007, 10:42:51 AM »
Another new awesome  skill!
Thank you so much, Danielm103.


TonyT

  • Guest
Re: ResultBuffers and LispFunction()
« Reply #34 on: April 10, 2007, 06:07:13 AM »

Thanks Dan. I posted a stripped down version of that class
because the remainder was dependent on other things that
would needed to have come along for the ride.

One thing you might note is that the class has implicit
conversions to/from ResultBuffer, TypedValue[], and
SelectionFilter.

That means that instead of:

Code: [Select]

TypedValueList arInputArgs = new TypedValueList(rbfArgs.AsArray());


You can just do:

Code: [Select]

TypedValueList arInputArgs = new TypedValueList(rbfArgs);

// Or for that matter, you can just do this:

TypedValueList arInputArgs = rbfArgs;

// Just to make that clearer:

   DBObject myObj = myTrans.GetObject(...);

   TypedValueList list = myObj.XData;  // assign directly to TypedValueList

   // modify list here

   myObj.XData = list;     // implicitly convert to a ResultBuffer




Also, on the typecasting from DxfCode and LispDataType,
business, you can also clean up code that uses that class,
by adding a few overloaded Add methods, like so:

Code: [Select]

    public void Add(LispDataType type, object value)
    {
        Add( new TypedValue((int) type, value));
    }

    public void Add(DxfCode code, object value)
    {
        Add( new TypedValue((int) code, value));
    }


That means, you can add items to the list, more easily
and cleanly, at a slight cost:

Code: [Select]

     TypedValueList list = new TypedValueList();

     list.Add( LispDataType.ListBegin, 0 );
     list.Add( LispDataType.Double, 99.000 );
     list.Add( LispDataType.String, "hello world" );
     list.Add( LispDataType.ListEnd, 0 );


With those two overloads, there's no need to create
TypedValues to add items to the list, or typecast from
either DxfCode or LispDataType.

Lastly, another improvement is to overload the constructor
to take IEnumerable arguments, which allows you to create
a new instance that's populated from any type that can
enumerate TypedValues (e.g. ResultBuffer, TypedValue[],
ArrayList, List<TypedValue>, or another TypedValueList):

Code: [Select]

   public TypedValueList( IEnumerable<TypedValue> source )
   {
      AddRange( source );
   }

   public TypedValueList( IEnumerable source )
   {
      foreach( TypedValue v in source )
         Add( v );
   }


How about using TonyT’s awesome TypedValueList class ?
http://www.theswamp.org/index.php?topic=14495.msg186823#msg186823

Code: [Select]
[LispFunction("PolarPoint")]
    public ResultBuffer DotNetPolarPoint(ResultBuffer rbfArgs)
    {
      //TonyT's cool TypedValueList
      TypedValueList arInputArgs = new TypedValueList(rbfArgs.AsArray());
      TypedValueList arOutputArgs = new TypedValueList();
      //
      Point3d co = (Point3d)arInputArgs[0].Value;
      double ang = (double)arInputArgs[1].Value;
      double dst = (double)arInputArgs[2].Value;
      //
      arOutputArgs.Add((int)LispDataType.Point3d,
                       new Point3d(co.X +
                       (dst * Math.Cos(ang)),
                       co.Y + (dst * Math.Sin(ang)),
                       co.Z));

      return arOutputArgs; //edit
    }

This seems to be the way to go, thanks Tony

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8696
  • AKA Daniel
Re: ResultBuffers and LispFunction()
« Reply #35 on: April 10, 2007, 07:01:50 AM »
Fantastic! Thanks Tony

Also for those who are watching, this constructor overload might be handy for args like

Code: [Select]
LispDataType.T_atom

LispDataType.Nil

Code: [Select]
public void Add(LispDataType type)
    {
      Add(new TypedValue((int)type));
    }