// CodeHimBelonga kdub@theSwamp
//
#region UsingRegion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using ACAD = Autodesk.AutoCAD.Interop;
using ACADCOM = Autodesk.AutoCAD.Interop.Common;
#endregion
[assembly
: CommandClass
(typeof(kdub_Testing
.MyCommandsClass))]
namespace kdub_Testing
{
public class MyCommandsClass
{
Document dwg;
Database db;
Editor ed;
//
public MyCommandsClass()
{
ActiveDrawing = AcadApp.DocumentManager.MdiActiveDocument;
}
Document ActiveDrawing
{
get { return dwg; }
set
{
dwg = value;
if(dwg != null) {
db = dwg.Database;
ed = dwg.Editor;
} else { db = null; ed = null; }
}
}
//
// Type of resbuf element
// credit to Alexander Rivilis
const short RTNONE = 5000; /* No result */
const short RTREAL = 5001; /* Real number */
const short RTPOshort = 5002; /* 2D poshort X and Y only */
const short RTSHORT = 5003; /* Short integer */
const short RTANG = 5004; /* Angle */
const short RTSTR = 5005; /* String */
const short RTENAME = 5006; /* Entity name */
const short RTPICKS = 5007; /* Pick set */
const short RTORshort = 5008; /* Orientation */
const short RT3DPOshort = 5009; /* 3D poshort - X, Y, and Z */
const short RTLONG = 5010; /* Long integer */
const short RTVOID = 5014; /* Blank symbol */
const short RTLB = 5016; /* list begin */
const short RTLE = 5017; /* list end */
const short RTDOTE = 5018; /* dotted pair */
const short RTNIL = 5019; /* nil */
const short RTDXF0 = 5020; /* DXF code 0 for ads_buildlist only */
const short RTT = 5021; /* T atom */
const short RTRESBUF = 5023; /* resbuf */
const short RTMODELESS = 5027; /* interrupted by modeless dialog */
//
// Error return code
const short RTNORM = 5100; /* Request succeeded */
const short RTERROR = -5001; /* Some other error */
const short RTCAN = -5002; /* User cancelled request -- Ctl-C */
const short RTREJ = -5003; /* AutoCAD rejected request -- invalid */
const short RTFAIL = -5004; /* Link failure -- Lisp probably died */
const short RTKWORD = -5005; /* Keyword returned from getxxx() routine */
const short RTINPUTTRUNCATED = -5008; /* Input didn't all fit in the buffer */
///
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe",
EntryPoint = "acedPutSym",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)
]
extern static private int acedPutSym(string args, IntPtr result);
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe",
EntryPoint = "acedGetSym",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)
]
extern static private int acedGetSym(string args, out IntPtr result);
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns>ResultBuffer</returns>
internal static ResultBuffer GetLispSym(string name)
{
IntPtr ip = IntPtr.Zero;
if((acedGetSym(name, out ip) == RTNORM) && (ip != IntPtr.Zero)) {
return ResultBuffer.Create(ip, true);
}
return null;
}
/// <summary>
///
/// </summary>
[CommandMethod("DoIt1114a", CommandFlags.Modal)]
public void DoIt1114a()
{
ResultBuffer res1 = GetLispSym("TestVar1");
ResultBuffer res2 = GetLispSym("TestVar2");
ResultBuffer res3 = GetLispSym("TestVar3");
ResultBuffer res4 = GetLispSym("TestVar4");
ed.WriteMessage("{0} : {1}", "\nRes1", res1.ToString());
ed.WriteMessage("{0} : {1}", "\nRes2", res2.ToString());
ed.WriteMessage("{0} : {1}", "\nRes3", res3.ToString());
ed.WriteMessage("{0} : {1}", "\nRes4", res4.ToString());
if(res4 != null) {
// credit to Alexander Rivilis
StringBuilder s
= new StringBuilder
(); s.Append("\n******");
foreach(TypedValue val in (System.Collections.IEnumerable)res4) {
s.AppendFormat("\n{0} -> {1}", val.TypeCode, val.Value.ToString());
}
s.Append("\n******");
ed.WriteMessage(s.ToString());
}
}
/// <summary>
///
/// </summary>
[CommandMethod("DoIt1114b", CommandFlags.Modal)]
public void DoIt1114b()
{
PromptResult res = ed.GetString("\nName of lisp-variable: ");
string LispVarName = string.Empty;
if(res.Status == PromptStatus.OK) {
ResultBuffer rb
= new ResultBuffer
(); LispVarName = res.StringResult;
rb
.Add(new TypedValue
(RTSTR,
"This is the transported Text")); //
acedPutSym(LispVarName, rb.UnmanagedObject);
}
ed.WriteMessage("{0} : {1}", "\nRes1", GetLispSym(LispVarName).ToString());
}
/// <summary>
///
/// </summary>
[CommandMethod("DoIt1114c", CommandFlags.Modal)]
public void DoIt1114c()
{
/*
* Lets say we have an expectation that a Lisp List has a specific structure
* of (list String Integer RotationAngle 3dPoint)
*
* we need to build the ResultBuffer to suit.
*/
PromptResult resName = ed.GetString("\nName of lisp-variable: ");
string LispVarName = resName.StringResult;
PromptResult resStr = ed.GetString("\nInput String Value : ");
PromptIntegerResult resInt = ed.GetInteger("\nInput Integer Value : ");
PromptDoubleResult resAng = ed.GetAngle("\nInput or select Angle : ");
PromptPointResult resPt = ed.GetPoint("\nInput or Select 3DPoint : ");
// For the exercise, assume the values are Valid .... 'danger Will Robinson'
//
// Use Tony's TypedValueList class to build the result buffer.
TypedValueList dataList
= new TypedValueList
();
dataList.Add(LispDataType.ListBegin, 0);
dataList.Add(LispDataType.Text, resStr.StringResult);
dataList.Add(LispDataType.Int32, resInt.Value);
dataList.Add(LispDataType.Angle, resAng.Value);
dataList.Add(LispDataType.Point3d, resPt.Value);
dataList.Add(LispDataType.ListEnd, 0);
// add the Variable to AutoCad
acedPutSym(LispVarName, ((ResultBuffer)dataList).UnmanagedObject);
// get the Lisp variable from AutoCAD
ResultBuffer rb1114c = GetLispSym(LispVarName);
if(rb1114c != null) {
// credit to Alexander Rivilis
StringBuilder s
= new StringBuilder
(); s.Append("\n******");
foreach(TypedValue val in (System.Collections.IEnumerable)rb1114c) {
s.AppendFormat("\n{0} -> {1}", val.TypeCode, val.Value.ToString());
}
s.Append("\n******");
ed.WriteMessage(s.ToString());
}
}
}
}