yucad3d

Block clip (XCLIP)

Insert a block clipped to a rectangle — only the content inside draws, and the clip round-trips to AutoCAD as a SPATIAL_FILTER.

// Block clip (XCLIP): a 100x100 grid block clipped to a rectangle — only the
// content inside shows; Save DXF and the clip round-trips as SPATIAL_FILTER.
static void feat_blockclip() {
    samp_clear();
    int hBlock = CadAddBlock(g_hDwg, "CLIPGRID", 0, 0, 0);
    if (!hBlock) { set_status(L"CadAddBlock failed"); return; }
    for (int i = 0; i <= 10; ++i) {
        int a = CadAddLine(g_hDwg, i*10.0,0,0, i*10.0,100,0); CadBlockAddEntity(hBlock, a);
        int b = CadAddLine(g_hDwg, 0,i*10.0,0, 100,i*10.0,0); CadBlockAddEntity(hBlock, b);
    }
    int c = CadAddCircle(g_hDwg, 50,50,0, 40); CadBlockAddEntity(hBlock, c);
    int hIns = CadAddBlockClip(g_hDwg, "CLIPGRID", 25, 25, 50, 40);
    feat_rect_lines(25, 25, 75, 65);                     // red frame on the boundary
    for (int e = CadGetFirstEntity(g_hDwg); e; e = CadGetNextEntity(g_hDwg, e))
        if (e != hIns && CadEntityGetType(e) == CAD_ENT_LINE) CadEntityPutColor(e, CAD_COLOR_RED);
    CadUpdate(g_hDwg); samp_zoom_ext();
    set_status(hIns ? L"CadAddBlockClip: grid clipped to the red rect (Save DXF -> AutoCAD XCLIP)"
                    : L"CadAddBlockClip FAILED");
}
    3:                                            // Block clip (XCLIP)
      begin
        CadPurge(FhDwg, CAD_CLEAR_ENTITIES);
        hBlock := CadAddBlock(FhDwg, 'CLIPGRID', 0, 0, 0);
        if hBlock = 0 then begin StatusBar.SimpleText := 'CadAddBlock failed'; Exit; end;
        for i := 0 to 10 do
        begin
          e := CadAddLine(FhDwg, i * 10.0, 0, 0, i * 10.0, 100, 0); CadBlockAddEntity(hBlock, e);
          e := CadAddLine(FhDwg, 0, i * 10.0, 0, 100, i * 10.0, 0); CadBlockAddEntity(hBlock, e);
        end;
        c := CadAddCircle(FhDwg, 50, 50, 0, 40); CadBlockAddEntity(hBlock, c);
        hIns := CadAddBlockClip(FhDwg, 'CLIPGRID', 25, 25, 50, 40);
        FeatRectLines(25, 25, 75, 65);            // red frame on the boundary
        e := CadGetFirstEntity(FhDwg);
        while e <> 0 do
        begin
          if (e <> hIns) and (CadEntityGetType(e) = CAD_ENT_LINE) then
            CadEntityPutColor(e, CAD_COLOR_RED);
          e := CadGetNextEntity(FhDwg, e);
        end;
        CadUpdate(FhDwg); CadExecute(FhDwg, FCadHwnd, CAD_CMD_ZOOM_EXT);
        if hIns <> 0 then
          StatusBar.SimpleText := 'CadAddBlockClip: grid clipped to the red rect (Save DXF -> AutoCAD XCLIP)'
        else
          StatusBar.SimpleText := 'CadAddBlockClip FAILED';
      end;
    private void FeatBlockClip()
    {
        SampleClear();
        var hBlock = YuCadApi.CadAddBlock(_hDwg, "CLIPGRID", 0, 0, 0);
        if (hBlock == 0) { SetStatus("CadAddBlock failed"); return; }
        for (var i = 0; i <= 10; ++i)
        {
            var a = YuCadApi.CadAddLine(_hDwg, i * 10.0, 0, 0, i * 10.0, 100, 0); YuCadApi.CadBlockAddEntity(hBlock, a);
            var b = YuCadApi.CadAddLine(_hDwg, 0, i * 10.0, 0, 100, i * 10.0, 0); YuCadApi.CadBlockAddEntity(hBlock, b);
        }
        var c = YuCadApi.CadAddCircle(_hDwg, 50, 50, 0, 40); YuCadApi.CadBlockAddEntity(hBlock, c);
        var hIns = YuCadApi.CadAddBlockClip(_hDwg, "CLIPGRID", 25, 25, 50, 40);
        FeatRectLines(25, 25, 75, 65);          // red frame on the clip boundary
        for (var e = YuCadApi.CadGetFirstEntity(_hDwg); e != 0; e = YuCadApi.CadGetNextEntity(_hDwg, e))
            if (e != hIns && YuCadApi.CadEntityGetType(e) == YuCadApi.CAD_ENT_LINE)
                YuCadApi.CadEntityPutColor(e, YuCadApi.CAD_COLOR_RED);
        YuCadApi.CadUpdate(_hDwg); SampleZoomExt();
        SetStatus(hIns != 0 ? "CadAddBlockClip: grid clipped to the red rect (Save DXF -> AutoCAD XCLIP)"
                            : "CadAddBlockClip FAILED");
    }
This code is extracted verbatim from the shipped demo hosts (examples/cpp, examples/delphi, examples/csharp) — run the matching menu item there to see it live.