|
Create an anonymous group from Visual LISP with help from ARX
By Gopinath Taget
The following ARX code does that. You need to however register the ARX function using acedDefun() to be able to use it as an external Lisp function. It is implemented such that it returns a list, containing the new group's ObjectID, to the external Lisp function.
[code]int acdbAddAnonGrpRet2Lsp()
{
// TODO: Implement the command
resbuf *pArg =acedGetArgs ();
struct resbuf *startRb = NULL;
AcDbGroup *pGroup = new AcDbGroup();
AcDbObjectId grpId;
AcDbDictionary *pGroupDict;
Acad::ErrorStatus es;
AcApDocument *curDoc;
//get the group dictionary
AcDbDatabase *pDb = acdbHostApplicationServices()->
workingDatabase();
if (Acad::eOk == (es = acDocManager->lockDocument(
curDoc=acDocManager->curDocument())))
es = pDb->getGroupDictionary(pGroupDict,AcDb::kForWrite);
if (es != Acad::eOk)
return es;
//make an anonymous entry
if ((es = pGroupDict->setAt(L"*", pGroup, grpId)) ==
Acad::eOk) {
//retrieve its name char *pNam;
pGroup->close();
}
// create a resbuf with our ObjectID in it
struct resbuf *newRb = acutBuildList (RTLONG, grpId, RTNONE);
// if ok
if (newRb != NULL) {
// if this is the first time we've done this
if (startRb == NULL) {
// then set this as the start
startRb = newRb;
}
// otherwise add it to the end of our list
else {
// create a pointer to the beginning of our resbuf list
struct resbuf *ptr = startRb;
// find the end of our list
while (ptr->rbnext != NULL)
ptr = ptr->rbnext;
// now attach our newly create resbuf to the end
ptr->rbnext = newRb;
}
}
pGroupDict->close();
acDocManager->unlockDocument(curDoc);
acedRetList(startRb);
acutRelRb(startRb);
return (RTNORM);
}
The Lisp code:
(defun isAppLoaded (app)
(if (not (member app (arx)))
(setq bAppLoaded :vlax-false)
(setq bAppLoaded :vlax-true)
)
bAppLoaded
)
(defun Example_AddLightWeightPolyline()
(vl-load-com)
(if (= (isAppLoaded "asdkanonygrplsp.arx") :vlax-false)
(arxload (findfile "asdkanonygrplsp.arx"))
)
(setq oAcad (vlax-get-acad-object)
oDoc (vla-get-activedocument oAcad)
*ModelSpace* (vla-get-ModelSpace oDoc)
)
;;Add anonymous group
(setq myGroup (vl-catch-all-apply
'vla-objectidtoobject
(list
oDoc
(vlax-make-variant
(car (acdbAddAnonGrpRet2Lsp))
vlax-vbLong
)
)
)
) ;setq
;;If error encountered making group, then exit
(if (vl-catch-all-error-p myGroup)
(princ (vl-catch-all-error-message myGroup))
;;otherwise continue...
(progn
;; Define the 2D polyline points
(setq pt1 (list 1.0 1.0)
pt2 (list 1.0 2.0)
pt3 (list 2.0 2.0)
pt4 (list 3.0 2.0)
pt5 (list 4.0 4.0)
)
(setq Points (apply 'append (list pt1 pt2 pt3 pt4 pt5)))
(setq ptlstlen (length Points))
;; Make array of 5*2=10 doubles -
;; that's an array of dimension 0 to 9
(setq PointDataA (
vlax-make-safearray vlax-vbDouble (
cons 0 (1- ptlstlen))))
(vlax-safearray-fill PointDataA Points)
(setq PointData (vlax-make-variant PointDataA))
;; Create a light weight Polyline object in model space
(setq myLWpoly (
vla-addLightweightPolyline *ModelSpace* PointData))
;; Make array of 1 object (the new LWPolyline)
(setq myObjA (vlax-make-safearray vlax-vbObject '(0 . 0)))
(vlax-safearray-put-element myObjA 0 myLWpoly)
;; Add the LWPolyline to the group
(vla-appenditems myGroup myObjA)
) ;progn
) ;if
(princ)
)[/code] |
|