View Full Version : changevpsettings .Net to LISP function
peter
2009-12-11, 04:02 AM
I took the AU class on how to extend LISP with .net.
I converted the existing .net functions from AutoCAD2008 to AutoCAD2010.
In the examples was one called changevpsettings that accepted an objectID, layername and color. It changed the color of a given layer given the objectID of the viewport.
I pass it (the function) an objectID and instead of its lispdatatype.objectid it interprets it as a lispdatatype.Int32 and the function crashes.
Have any of you played with this routine? Do any of you know how to change an resbuf as an array item from an int32 to an objectID?
Peter
_gile
2009-12-11, 02:28 PM
Hi,
While using an ObjectId in the resbuf of an a LispFunction argument, the LISP argument have to be an ENAME
A little sample which put the entity refered by its ObjectId in ColorIndex 1
Using example:
(entred (car (entsel)))
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace LispFunctionTest
{
public class Class1
{
[LispFunction("EntRed")]
public void test(ResultBuffer resbuf)
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
if (resbuf == null)
{
ed.WriteMessage("\nError: too few arguments\n");
return;
}
TypedValue[] args = resbuf.AsArray();
if (args.Length == 0)
{
ed.WriteMessage("\nError: too few arguments\n");
return;
}
if (args.Length > 1)
{
ed.WriteMessage("\nError: too much arguments\n");
return;
}
if (args[0].TypeCode != LispDataType.ObjectId)
{
ed.WriteMessage("\nError: incorrect argument\n");
return;
}
ObjectId id = (ObjectId)args[0].Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(id, OpenMode.ForWrite, false) as Entity;
ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
tr.Commit();
}
}
}
}
peter
2009-12-11, 02:37 PM
I knew it had to be something simple.
Thanks
Peter
_gile
2009-12-11, 03:44 PM
You're welcome Peter,
I wanted to look a little further (I'm .NET newby).
If you want your LispFunction to work both with ename or integer argument (OldId), it semms it's possible.
You can get a .NET ObjectID from an integer using the ObjectId constructor:
ObjectId id;
switch (args[0].TypeCode)
{
case (short)LispDataType.ObjectId:
id = (ObjectId)args[0].Value;
break;
case (short)LispDataType.Int32:
id = new ObjectId((int)args[0].Value);
break;
default:
ed.WriteMessage("\nError: incorrect argument\n");
return;
}
if (id.IsValid)
{
// Do your stuff here
}
This way both:
(MyLispFunction (car (entsel)))
and:
(MyLispFunction (vla-get-ObjectId (vlax-ename->vla-object (car (entsel)))))
should work.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.