How to insert blocks in CAD.net and how we can extend them

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 20 Jun 2019, 20:25

Hi,

I am new in CAD.net can you please assist me how can I insert a block and it's entities in CAD.net. Is there any way to extend them in current drawing.

Looking forward for quick response.


Thanks,
Shiv

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to insert blocks in CAD.net and how we can extend them

Post by support » 20 Jun 2019, 21:15

Hello Shiv,

To insert a block (CADBlock object) in a certain drawing layout (e.g. the current layout), you will need to create a CADInsert object instance which refers to the block, load the CADInsert into the drawing database by calling a CADConverter.Loads method and then place the insert onto some CADLayout by calling a CADLayout.AddEntity method.

The code example below shows how to insert a block with a given name at a certain point on the current drawing layout.

Code: Select all

public static void InsertBlock(CADImage cadImage, string blockName, string layerName, DPoint insPoint, int width, int height, float angle)
{
    if (cadImage != null)
    {
        CADBlock block = cadImage.Converter.BlockByName(blockName);
        if (block != null)
        {
            CADInsert insert = new CADInsert();
            insert.Block = block;
            insert.Layer = cadImage.Converter.LayerByName(layerName);
            insert.Point = insPoint;

            double scaleX, scaleY;
            if (width == 0)
                scaleX = 1;
            else
                scaleX = width / (block.Box.right - block.Box.left);
            if (height == 0)
                scaleY = 1;
            else
                scaleY = width / (block.Box.top - block.Box.bottom);

            insert.Scale = new DPoint(scaleX, scaleY, 1);
            insert.Angle = angle;
            cadImage.Converter.Loads(insert);
            cadImage.CurrentLayout.AddEntity(insert);
        }
    }
}
Could you please elaborate on "extend the blocks" task?

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 21 Jun 2019, 08:59

Thanks, Mikhail for your help.

Below is the Elaboration of the extension of the block task.

we just want to check if we want to extend the CAD.net Block or use our own block. If we use our own, how do we reference theirs? We need to support custom fields and tags in blocks and be able to serialize them.

Please let me know if you have any questions.

Thanks,
Shiv

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to insert blocks in CAD.net and how we can extend them

Post by support » 21 Jun 2019, 20:05

Hello Shiv,

Custom fields can be added to a CADBlock object in CAD .NET through an CADAttdef class. Each CADAttdef instance represents an attribute definition which may be used as a custom field with a certain tag (CADAttdef.Tag) and value (CADAttdef.Value).

One CADBlock object may contain one or few CADAttdef objects, they are stored in a CADBlock.Entities collection along with the block entities.

I have one question regarding serialization: do you need to be able to serialize the custom fields by using a BinaryFormatter.Serialize method to memory, a database or a file?

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 22 Jun 2019, 14:26

Thanks a lot for detailed information, Mikhail. Let me check this out and get back to you.

Regards,
Shiv

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 25 Jun 2019, 08:07

Hi Mikhail,

We will be serializing to memory for now, and then uploading the objects to the cloud server. For performance, we might need to serialize to a local DB in the future, but we don't need that immediately.

Thanks,
Shiv

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to insert blocks in CAD.net and how we can extend them

Post by support » 25 Jun 2019, 18:50

Hello Shiv,

I would also like to clarify the following: do you need to serialize only custom fields (block attributes) or the whole CADBlock object including the block attributes and block entities?

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 06 Jul 2019, 14:08

Thank you, Mikhail, for your help and sorry for the late reply;

I was trying to create a new block in my current drawing using the code which you have suggested to me earlier and I am getting the "Object reference not set an instance of an object" in the very second line of the code.

Please refer my code below;

private string blockname = "Myblock";

public static void InsertBlock(CADImage cadImage, string blockName, string layerName, DPoint insPoint, int width, int height, float angle)
{
if (cadImage != null)
{
CADBlock block = cadImage.Converter.BlockByName(blockName);

"At this point, I am getting the block value null.

if (block != null)
{
CADInsert insert = new CADInsert();
insert.Block = block;
insert.Layer = cadImage.Converter.LayerByName(layerName);
insert.Point = insPoint;

double scaleX, scaleY;
if (width == 0)
scaleX = 1;
else
scaleX = width / (block.Box.right - block.Box.left);
if (height == 0)
scaleY = 1;
else
scaleY = width / (block.Box.top - block.Box.bottom);

insert.Scale = new DPoint(scaleX, scaleY, 1);
insert.Angle = angle;
cadImage.Converter.Loads(insert);
cadImage.CurrentLayout.AddEntity(insert);
}
}
}

Thanks again for your always kind support!

Please suggest.

Regards,
Shiv

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 08 Jul 2019, 14:05

Hi, Mikhail,

Could you please help me with this? I am really got stuck bcz of this issue.

Looking forward to your quick response.



Thanks,
Shiv

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to insert blocks in CAD.net and how we can extend them

Post by support » 08 Jul 2019, 19:59

Hello Shiv,

The problem is that the block definition (CADBlock) with the name "Myblock" cannot be found in the passed CADImage instance. The InsertBlock method inserts an existing block definition, it doesn't create a new one.

To create a new block defintion, please do the following:
  1. Create the CAD entities for the block definition (or pick the existing ones from a CADImage.Converter.Entities collection).
  2. Create a new CADBlock instance.
  3. Specify a name for the block definition through a CADBlock.Name property.
  4. Add the created (or picked) CAD entities to the block definition using a CADBlock.AddEntity method.
  5. Load the block definition into the BLOCKS section using the following code:

    Code: Select all

    cadImage.Converter.Loads(block);
    cadImage.Converter.GetSection(FaceModule.ConvSection.Blocks).AddEntity(block);
    
Please note that the block definition is not visible until you insert it.

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 09 Jul 2019, 15:46

Thank you, Mikhail, for your help. Is there any way to check if my block is created successfully or not? Also, I want to add a validation check for the duplicate blocks(i.e. if my block with the same name already exists in the drawing and if I try to create the same block with the same name again then it should restrict me for creating the duplicate block).


Regards,
Shiv

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to insert blocks in CAD.net and how we can extend them

Post by support » 09 Jul 2019, 19:37

Hello Shiv,

You may use a CADConverter.BlockByName method to check if a block with the given name already exists. This question was discussed in the given topic.

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

shivajitheboss
Posts: 32
Joined: 20 Jun 2019, 16:40

Re: How to insert blocks in CAD.net and how we can extend them

Post by shivajitheboss » 10 Jul 2019, 08:00

Thanks for the reply, Mikhail.

Regards,
Shiv

Post Reply