Code sample Home

Retrieve position on a polyline by mouse click.

The below function is called on the Left button down event.
When user clicks (with pressed <Ctrl>) on a polyline object, the function find coordinates on the curve and insert a point on this place.

void DemoPlineLBDown (HANDLE hEvent)
{
  HANDLE hLcWnd, hDrw, hBlock, hPtStyle, hPline, hPnt;
  int    EntType;
  BOOL   bCtrl;
  double X, Y, X2, Y2, Dist, D2p;
  WCHAR* szName = L"MyPtStyle";

  // get state of <Ctrl> key
  bCtrl = lcPropGetInt( hEvent, LC_PROP_EVENT_INT4 );
  if (bCtrl == false){
    return;
  }
  
  //------- This part is used to set style for new points
  // get drawing handle
  hDrw = lcPropGetHandle( hEvent, LC_PROP_EVENT_DRW );
  // set active point style
  hPtStyle = lcDrwGetObjectByName( hDrw, LC_OBJ_PNTSTYLE, szName );
  if (hPtStyle == 0){
    // the point style don't exist, create it
    hPtStyle = lcDrwAddPntStyle( hDrw, szName, 0, 1.0, 0, 1.0, 1.0 );
    lcPropPutInt( hPtStyle, LC_PROP_PSTYLE_PTMODE, LC_POINT_PLUS|LC_POINT_CIRCLE );
    lcPropPutFloat( hPtStyle, LC_PROP_PSTYLE_PTSIZE, -1.0 );
  }
  // set active point style
  lcPropPutHandle( hDrw, LC_PROP_DRW_PNTSTYLE, hPtStyle );
  //-------------------------

  // get window handle
  hLcWnd = lcPropGetHandle( hEvent, LC_PROP_EVENT_WND );
  // get block handle
  hBlock = lcPropGetHandle( hEvent, LC_PROP_EVENT_BLOCK );
  // get entity by cursor position
  hPline = lcWndGetEntByPoint( hLcWnd, -1, -1 );
  if (hPline != 0){
    // check the entity type
    EntType = lcPropGetInt( hPline, LC_PROP_ENT_TYPE );
    if (EntType == LC_ENT_POLYLINE){
      // get cursor coordinates
      X = lcPropGetFloat( hEvent, LC_PROP_EVENT_FLOAT1 );
      Y = lcPropGetFloat( hEvent, LC_PROP_EVENT_FLOAT2 );
      // by the click point, get position on RPlan curve
      D2p = lcPlineGetDist( hPline, X, Y, &X2, &Y2, &Dist );
      // at found position, add point entity
      hPnt = lcBlockAddPoint( hBlock, X2, Y2 );
      lcPropPutInt( hPnt, LC_PROP_ENT_COLOR, RGB(255,100,0) );
      lcBlockUpdate( hBlock, false, hPnt );
      lcWndRedraw( hLcWnd ); 
      // disable default Litecad reaction on the event
      lcEventReturnCode( 1 );
    }
  }
}

See Also:

Retrieve positions on a polyline by given distance from beginning