Does a block exist with Delphi

Does a block exist with Delphi

Anonymous
Not applicable
461 Views
5 Replies
Message 1 of 6

Does a block exist with Delphi

Anonymous
Not applicable
I'm using Delphi to try and find if a block exists in a drawing. The Logic
should be the same as VBA however, I think I'm missing something really
minor. What type of argument do I supply for the ThisDrawing.Blocks(?)

My code is:

var
blockRefObj : IAcadBlockReference;
sBlkName : widestring;
sACADPath : string;
begin
sACADPath := dtmGAFloatingOptions.appAcadApp.Path;//Path to AutoCAD
sBlkName := sACADPath+'\PREVRDGS.dwg';//Name of block to insert.
if blockRefObj =
dtmGAFloatingOptions.appAcadApp.ActiveDocument.Blocks(sBlkName) then
Showmessage ('Block exists');
end;
0 Likes
462 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
"Darrell" wrote in message
news:3C9C76F76CDEC8C4FEE2F6452A47FA24@in.WebX.maYIadrTaRb...
> I'm using Delphi to try and find if a block exists in a drawing. The
Logic
> should be the same as VBA however, I think I'm missing something really
> minor. What type of argument do I supply for the ThisDrawing.Blocks(?)
>
> My code is:
>
> var
> blockRefObj : IAcadBlockReference;
> sBlkName : widestring;
> sACADPath : string;
> begin
> sACADPath := dtmGAFloatingOptions.appAcadApp.Path;//Path to AutoCAD
> sBlkName := sACADPath+'\PREVRDGS.dwg';//Name of block to insert.
> if blockRefObj =
> dtmGAFloatingOptions.appAcadApp.ActiveDocument.Blocks(sBlkName) then
> Showmessage ('Block exists');
> end;
>

In Delphi, you can't omit the name of the default property
(usually 'Item()' for collections) unless you make a few
changes to the type library import unit.

So, where you use Document.Blocks(blockname) in VBA, you
must use Document.Blocks.Item(Blockname) in Delphi.

This function will return True if a block exists:

Function BlockExists(Doc: AcadDocument; BlockName: String): Boolean
begin
try
Doc.Blocks.Item(BlockName);
Result := True;
except
Result := False;
end;
end;
0 Likes
Message 3 of 6

Anonymous
Not applicable
var

blockRefObj : IACADBlockReference;

Begin

if BlockExists(currDwg, BlockName) then

blockRefObj := currDwg.Blocks.????

End;

"Darrell" wrote in message
news:3C9C76F76CDEC8C4FEE2F6452A47FA24@in.WebX.maYIadrTaRb...
> I'm using Delphi to try and find if a block exists in a drawing. The
Logic
> should be the same as VBA however, I think I'm missing something really
> minor. What type of argument do I supply for the ThisDrawing.Blocks(?)
>
> My code is:
>
> var
> blockRefObj : IAcadBlockReference;
> sBlkName : widestring;
> sACADPath : string;
> begin
> sACADPath := dtmGAFloatingOptions.appAcadApp.Path;//Path to AutoCAD
> sBlkName := sACADPath+'\PREVRDGS.dwg';//Name of block to insert.
> if blockRefObj =
> dtmGAFloatingOptions.appAcadApp.ActiveDocument.Blocks(sBlkName) then
> Showmessage ('Block exists');
> end;
>
>
0 Likes
Message 4 of 6

Anonymous
Not applicable
You're confusing block references and blocks.

Block references (AcadBlockReference) are created with
the INSERT command.

Blocks (AcadBlock) are created with the BLOCK command.

What exactly are you trying to get (a block, or a
block reference) ?

"Darrell" wrote in message
news:582595DD221186AFEF3DF0AE377EF30A@in.WebX.maYIadrTaRb...
> var
>
> blockRefObj : IACADBlockReference;
>
> Begin
>
> if BlockExists(currDwg, BlockName) then
>
> blockRefObj := currDwg.Blocks.????
>
> End;
>
> "Darrell" wrote in message
> news:3C9C76F76CDEC8C4FEE2F6452A47FA24@in.WebX.maYIadrTaRb...
> > I'm using Delphi to try and find if a block exists in a drawing. The
> Logic
> > should be the same as VBA however, I think I'm missing something really
> > minor. What type of argument do I supply for the ThisDrawing.Blocks(?)
> >
> > My code is:
> >
> > var
> > blockRefObj : IAcadBlockReference;
> > sBlkName : widestring;
> > sACADPath : string;
> > begin
> > sACADPath := dtmGAFloatingOptions.appAcadApp.Path;//Path to AutoCAD
> > sBlkName := sACADPath+'\PREVRDGS.dwg';//Name of block to insert.
> > if blockRefObj =
> > dtmGAFloatingOptions.appAcadApp.ActiveDocument.Blocks(sBlkName) then
> > Showmessage ('Block exists');
> > end;
> >
> >
>
>
0 Likes
Message 5 of 6

Anonymous
Not applicable
I have a function that inserts a block into a drawing
blockRefObj := currDwg.ModelSpace.InsertBlock(varInsPoint,
sBlkName,sXScale,sXScale,sXScale,0.0);

but first, I check too see if the block is already inserted in the drawing
if BlockExists (currDwg,sBlkName)then

If the block already exists then just modify the attributes, if not, then
insert it and modify the attributes.

"Tony Tanzillo" wrote in message
news:DBDEF14F2818467C9C70D47B40F4A677@in.WebX.maYIadrTaRb...
> You're confusing block references and blocks.
>
> Block references (AcadBlockReference) are created with
> the INSERT command.
>
> Blocks (AcadBlock) are created with the BLOCK command.
>
> What exactly are you trying to get (a block, or a
> block reference) ?
>
> "Darrell" wrote in message
> news:582595DD221186AFEF3DF0AE377EF30A@in.WebX.maYIadrTaRb...
> > var
> >
> > blockRefObj : IACADBlockReference;
> >
> > Begin
> >
> > if BlockExists(currDwg, BlockName) then
> >
> > blockRefObj := currDwg.Blocks.????
> >
> > End;
> >
> > "Darrell" wrote in message
> > news:3C9C76F76CDEC8C4FEE2F6452A47FA24@in.WebX.maYIadrTaRb...
> > > I'm using Delphi to try and find if a block exists in a drawing. The
> > Logic
> > > should be the same as VBA however, I think I'm missing something
really
> > > minor. What type of argument do I supply for the
ThisDrawing.Blocks(?)
> > >
> > > My code is:
> > >
> > > var
> > > blockRefObj : IAcadBlockReference;
> > > sBlkName : widestring;
> > > sACADPath : string;
> > > begin
> > > sACADPath := dtmGAFloatingOptions.appAcadApp.Path;//Path to AutoCAD
> > > sBlkName := sACADPath+'\PREVRDGS.dwg';//Name of block to insert.
> > > if blockRefObj =
> > > dtmGAFloatingOptions.appAcadApp.ActiveDocument.Blocks(sBlkName) then
> > > Showmessage ('Block exists');
> > > end;
> > >
> > >
> >
> >
>
>
0 Likes
Message 6 of 6

Anonymous
Not applicable
"Darrell" wrote in message
news:2D9F5B301FFE4FC629028F6D4EBE251D@in.WebX.maYIadrTaRb...
> I have a function that inserts a block into a drawing
> blockRefObj := currDwg.ModelSpace.InsertBlock(varInsPoint,
> sBlkName,sXScale,sXScale,sXScale,0.0);
>
> but first, I check too see if the block is already inserted in the drawing
> if BlockExists (currDwg,sBlkName)then
>
> If the block already exists then just modify the attributes, if not, then
> insert it and modify the attributes.

You're still confusing blocks and block references.

The BlockExists() function tells you if the block
exists, it does not tell you if one or more insertions
of the block exists.

It sounds like you need to locate the existing
insertion of the block and if found, modify its
attributes. Otherwise, you would insert it.

To find the existing insertion, you will need to
use a selection set filter that creates a selection
set that contains all insertions of the block, and
then what you do from there is up to you.

var
ss: AcadSelectionSet;
BlockRef: AcadBlockReference;
Doc: AcadDocument;
vaType, vaData: OleVariant;
begin
vaType := VarArrayCreate([0, 1], varSmallInt);
vaType[0] := 0; vaType[1] := 2;
vaData := ArrayOf(["INSERT", "MYBLOCKNAME"]);
Doc = <>
ss := Doc.PickFirstSelectionSet;
ss.Clear;
ss.Select(acSelectionSetAll, EmptyParam,
EmptyParam, vaType, vaData);
if ss.Count = 0 then
BlockRef := Doc.ModelSpace.InsertBlock(...)
else
BlockRef := ss.Item(0) As AcadBlockReference;

// At this point, BlockRef is either the existing
// insertion of the block or a new insertion of it
// This of course assumes that there will be only
// one insertion of the block in the drawing, but
// if there are more than one, what you do about
// that is up to you.

end;
0 Likes