Author Topic: Shortening a set of AutoCAD lines using ARX  (Read 9870 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
Re: Shortening a set of AutoCAD lines using ARX
« Reply #15 on: July 19, 2010, 09:23:11 PM »
    //Paul this is a loop of objectIds

Getting under your skin a bit. Then let me just offer this final token, it is, "self documenting code" and comment the none obvious parts.

Ciao

a port of this


Code: [Select]
//...

template<class T> inline void destroy(T*& p) { delete p; p = 0; }

//...
    static AcGeVector3d delta (AcGePoint3d sp,AcGePoint3d ep)
  {
    acdbWcs2Ucs(asDblArray(sp),asDblArray(sp),false);
    acdbWcs2Ucs(asDblArray(ep),asDblArray(ep),false);
    return AcGeVector3d(ep.x - sp.x,ep.y - sp.y,ep.z - sp.z);
  }

  static Acad::ErrorStatus selectLines(AcDbObjectIdArray &ids)
  {
    Acad::ErrorStatus es = Acad::eOk;
    // ssname
    ads_name ssname = {0,0};

    //build our filter on the stack so we don't have a ptr to free
    resbuf filter = {NULL,RTDXF0};
    filter.resval.rstring = _T("LINE");

    // a nice happy message
    const TCHAR *msg[] = {_T("\nSelect Lines: "), _T("\nRemove Lines: ")};

    // get set, note the :$
    if(acedSSGet(_T(":$"),msg,0,&filter,ssname)!= RTNORM)
      return Acad::eNullObjectPointer;

    // get the ids
    es = acedGetCurrentSelectionSet(ids);

    acedSSFree(ssname);
    return es;
  }


  // - ArxExLine._exl command (do not rename)
  static void ArxExLine_exl(void)
  {
    AcDbObjectIdArray objectIds;
    if(selectLines(objectIds) != eOk)
      return;

    AcGePoint3d startPoint,endPoint;

    //Paul this is a loop of objectIds
    for(int idx = 0; idx < objectIds.length() ; idx++)
    {
      // use a smart pointer
      AcDbObjectPointer<AcDbLine> pLn(objectIds[idx], AcDb::kForWrite);
      if(pLn.openStatus() == eOk)
      {
        // get the start and end points
        pLn->getStartPoint(startPoint);
        pLn->getEndPoint(endPoint);

        // We'll extend our line by a quarter of the
        // existing length in each direction
        AcGeVector3d vext = delta(startPoint,endPoint) / 4;

        // extend the line
        pLn->extend(true, startPoint - vext);

        // extend the line, but don't extend the start
        pLn->extend(false, endPoint + vext);
      }
    }
  }

  // - ArxExLine._shl command (do not rename)
  static void ArxExLine_shl(void)
  {
    AcDbObjectIdArray objectIds;
    if(selectLines(objectIds) != eOk)
      return;

    double startParam=0;
    double endParam=0;
    double delta=0;

    AcGeDoubleArray paramArray;
    AcGeVoidPointerArray entPtrArray;

    //Paul this is a loop of objectIds
    for(int idx = 0; idx < objectIds.length() ; idx++)
    {
      // use a smart pointer
      AcDbObjectPointer<AcDbLine> pCurve(objectIds[idx], AcDb::kForWrite);
      if(pCurve.openStatus() == eOk)
      {

        // get the start and end params
        pCurve->getStartParam(startParam);
        pCurve->getEndParam(endParam);

        delta = (endParam-startParam) * 0.25;

        // add the params to our array for getSplitCurves
        paramArray.append(startParam + delta);
        paramArray.append(endParam - delta);

        if(pCurve->getSplitCurves(paramArray,entPtrArray)== eOk)
        {
          if (entPtrArray.length() == 3)
          {

            // See the arx docs for handOverTo
            AcDbEntity *newent = (AcDbEntity*)entPtrArray[1];
            pCurve->handOverTo(newent, true, true);
            newent->close();

            // delete the other new entities we don't need
            AcDbEntity *tmpent = (AcDbEntity*)entPtrArray[0];
            destroy(tmpent);

            tmpent = (AcDbEntity*)entPtrArray[2];
            destroy(tmpent);

            pCurve.release((AcDbLine*&)tmpent);
            destroy(tmpent);
          }
          else
          {
            // just-in-case do clean up
            for(int i = 0; i < entPtrArray.length() ; i++)
            {
              AcDbEntity *tmpent = (AcDbEntity*)entPtrArray[i];
              destroy(tmpent);
            }
          }
        }
        paramArray.removeAll();
        entPtrArray.removeAll();
      }
    }
  }

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
Re: Shortening a set of AutoCAD lines using ARX
« Reply #16 on: July 19, 2010, 09:31:20 PM »
No No No!!! You didn't get me ruffled up. I appreciate your bashing my code! I just had to throw in the bit of humor... well at least I hope you see it that way! please bash away, else I can't learn.   :smack:
Retired

pkohut

  • Guest
Re: Shortening a set of AutoCAD lines using ARX
« Reply #17 on: July 19, 2010, 09:36:46 PM »
All right, good to know. Cause I thought, "oops, I've crossed a line". It's easy to do when you're not communicating face to face.