Code sample Home

Find the intersection points of objects

Legend:
- a drawing has a set of relief isolines on layer "Isolines",
- a section line are placed on layer "0" and has a key "123" (in order to locate the line)
- we find points of intersection of the line with isolines
- at place of intersection points we will add a Point entity and Text entity.
The Text entity is unscalable, i.e. has constant size on a screen when a drawing is being scaled.

-------------------------------------------------
void DemoIntersect (HANDLE hLcWnd)
{
  HANDLE hDrw, hBlock, hEnt, hEnt2, hLayer, hLayer2, hTStyle, hPtStyle, hPnt, hLabel;
  int    i, j, n;
  double x, y;
  WCHAR  szNum[16];
  WCHAR* szColor = L"255,0,0";

  // get drawing and block, linked with CAD window
  hDrw = lcPropGetHandle( hLcWnd, LC_PROP_WND_DRW );
  hBlock = lcPropGetHandle( hLcWnd, LC_PROP_WND_BLOCK );
  // get layer "Isolines"
  hLayer = lcDrwGetObjectByName( hDrw, LC_OBJ_LAYER, L"Isolines" );
  // get textstyle "Labels"
  hTStyle = lcDrwGetObjectByName( hDrw, LC_OBJ_TEXTSTYLE, L"Labels" );
  // get an entity which intersect isolines, it have a key "123"
  hEnt = lcBlockGetEntByKey( hBlock, 123 );
  if (hLayer && hEnt && hTStyle){
    // get current point style
    hPtStyle = lcPropGetHandle( hDrw, LC_PROP_DRW_PNTSTYLE );
    // set points appearance (filled circle)
    lcPropPutInt( hPtStyle, LC_PROP_PSTYLE_PTMODE, LC_POINT_CIRCLE | LC_POINT_FILLED );
    // point size, positive value - drawing's units, negative - nonscalable size
    lcPropPutFloat( hPtStyle, LC_PROP_PSTYLE_PTSIZE, -5.0 );   
    // meaning of negative LC_PROP_PSTYLE_PTSIZE: true - pixels; false - % of window height
    lcPropPutBool( 0, LC_PROP_G_PNTPIXSIZE, true );  
    // make textstyle "Labels" current, in order to create new text entities with this style
    lcPropPutHandle( hDrw, LC_PROP_DRW_TEXTSTYLE, hTStyle );
    j = 1;
    // enumerate entities
    hEnt2 = lcBlockGetFirstEnt( hBlock );
    while( hEnt2 ){
      // get entity's layer
      hLayer2 = lcPropGetHandle( hEnt2, LC_PROP_ENT_LAYER );
      if (hLayer2 == hLayer){
        // find intersections, the function returns a number of intersection points
        n = lcIntersection( hEnt, hEnt2 );
        if (n > 0){
          for (i=0; i<n; i++){
            lcInterGetPoint( i, &x, &y );
            // add a point to a drawing
            hPnt = lcBlockAddPoint( hBlock, x, y );
            // draw it to red color
            lcPropPutStr( hPnt, LC_PROP_ENT_COLOR, szColor );
            lcPropPutStr( hPnt, LC_PROP_ENT_FCOLOR, szColor );
			
            // add a label text
            _itow( j++, szNum, 10 ); 
            // negative height value means pixel size instead of drawing units (to make text unscalable)
            hLabel = lcBlockAddText2( hBlock, szNum, x, y, LC_TA_CENBOT, -1, 0, 0, 0 );
            // set pixels offset from insertion point (only for unscalable text)
            lcPropPutInt( hLabel, LC_PROP_TEXT_DX, 0 );
            lcPropPutInt( hLabel, LC_PROP_TEXT_DY, 5 );
            // set the label's color
            lcPropPutStr( hLabel, LC_PROP_ENT_COLOR, L"cyan" );
          }
        }
      }
      // next entity
      hEnt2 = lcBlockGetNextEnt( hBlock, hEnt2 );
    }
    lcBlockUpdate( hBlock, true, 0 );
    lcWndRedraw( hLcWnd );
  }
}
Original drawing:



The drawing after executing the code:



Used functions:

lcDrwGetObjectByName
lcIntersection
lcInterGetPoint
lcWndRedraw

lcBlockGetEntByKey
lcBlockGetFirstEnt
lcBlockGetNextEnt
lcBlockAddPoint
lcBlockAddText2
lcBlockUpdate

lcPropGetHandle
lcPropPutInt
lcPropPutFloat
lcPropPutBool
lcPropPutHandle
lcPropPutStr