Hola, Glenn;
I have done like three or four lisp function with C#, but have not found a way to test if the arguments in the resbuf are the right ones, as what we can do in ARX.
Like:
ARX - to make sure the function has two arguments and both are ename's
if (pArgs && pArgs->restype == RTENAME && pArgs->rbnext && pArgs->rbnext->restype == RTENAME)
{
}
I have all the returned code values from the adscodes.h (there are some samples in the adesk net forum - from some of the ones that knowns C#) but have not done any real test as the one above.
And yes for example this function
[LispFunction("Summation")]
static public void summation(ResultBuffer args)
{
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;
// ignore start or end of list
short RTLB = 5016, RTLE = 5017;
if (val.TypeCode == RTLB || val.TypeCode == RTLE) { }
else
{
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);
}
}
}
}
foreach (KeyValuePair<string, short> kvp in openWith)
{
ed.WriteMessage("\nKey = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
If we, don't pass the right argument(s) it will throw something like:
Command: (summation "test")
System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.InvalidOperationException: Operation is
not valid due to the current state of the object.
at Autodesk.AutoCAD.DatabaseServices.ResultBufferEnumerator.get_Current()
at
Autodesk.AutoCAD.DatabaseServices.ResultBufferEnumerator.IEnumerator.get_Current
()
at ClassLibrary.LESQClass.summation(ResultBuffer args) in
C:\Programming\C#\AddTicks17\Class.cs:line 1153
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[]
arguments, SignatureStruct& sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[]
arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle
typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object
commandObject, Boolean bLispFunction)
at AcMgCommandClass.Invoke(AcMgCommandClass* ,
gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.TargetInvocationSEHExceptionFilter.InvokeWorker()
at Autodesk.AutoCAD.Runtime.ExceptionFilter.Invoke(); error: ADS request
error
Because requires a list of list's:
(setq lst (list '("one" 2) '("one" 3) '("two" 4) '("two" 5)))
Now, how do we check if we are passing the above to our function?
There are forms that might help, but I have not gone that far, cause I simple have not needed anything in lisp
- But, I am interested in the the know how....
Or inside of the: foreach (TypedValue rb in args) - grab or test what it is needed...
foreach (TypedValue rb in args)
{
switch ((LispDataType)rb.TypeCode)
{
case LispDataType.ListBegin:
ed.WriteMessage("\nListBegin");
break;
case LispDataType.ListEnd:
ed.WriteMessage("\nListEnd");
break;
case LispDataType.Text:
ed.WriteMessage("\nText");
break;
case LispDataType.Int16:
ed.WriteMessage("\nInt16");
break;
default:
ed.WriteMessage("\nOther [{0}] ", rb.Value);
break;
}
}
ed.WriteMessage("\n");
Maybe, if we first use a ResultBufferEnumerator and MoveNext() - verify all the items and once pass the test, use the Reset() and reuse the iterator....
Hope, makes some sense.... if not no problem