Please tell me how to insert Block.
@hardp,
Hello.
It depends on the initial format, target format, whether it is required to add totally new block or to use the existing one. The example is not trivial because of internal dependancies between different objects, but here is the basic idea to read DWG, add new block to it and export to PDF.
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
CadRasterizationOptions options = new CadRasterizationOptions();
options.Layouts = new string[] { "Model" };
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.VectorRasterizationOptions = options;
// search for available handle
string newObjectID = "FFFFFF";
if (cadImage.Header.HeaderProperties.ContainsKey(CadHeaderAttribute.HANDSEED))
{
newObjectID = ((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value;
int nextAvailableID = int.Parse(newObjectID, System.Globalization.NumberStyles.HexNumber) + 1;
((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value = nextAvailableID.ToString("X");
}
// new text object
CadText text = new CadText();
text.DefaultValue = "Sample text";
text.TextHeight = 40;
text.FirstAlignment = new Cad3DPoint(300, 40);
text.LayerName = "0";
// new insert object
CadInsertObject newInsert = new CadInsertObject();
newInsert.Name = "newInsert";
newInsert.ObjectHandle = newObjectID;
newInsert.LayerName = "0";
newInsert.InsertionPoint.X = 100;
newInsert.InsertionPoint.Y = 100;
newInsert.ScaleX = 2;
// add new text object to new block, add block to block entities
CadBlockDictionary allBlocks = cadImage.BlockEntities;
CadBlockEntity newBlock = new CadBlockEntity();
newBlock.AddEntity(text);
allBlocks.Add(newInsert.Name, newBlock);
cadImage.BlockEntities = allBlocks;
// add new insert to Entities
List<CadEntityBase> entities = new List<CadEntityBase>(cadImage.Entities);
entities.Add(newInsert);
cadImage.Entities = entities.ToArray();
// search for Model block and add new insert there too
CadBlockTableObject blockTableObjectReference = null;
CadBlockEntity cadBlockEntity = null;
foreach (CadBlockTableObject tableObject in cadImage.BlocksTables)
{
if (string.Equals(tableObject.HardPointerToLayout, cadImage.Layouts["Model"].ObjectHandle))
{
blockTableObjectReference = tableObject;
break;
}
}
if (blockTableObjectReference != null && cadImage.BlockEntities.ContainsKey(blockTableObjectReference.BlockName))
{
cadBlockEntity = cadImage.BlockEntities[blockTableObjectReference.BlockName];
}
List<CadEntityBase> blockEntities = new List<CadEntityBase>(cadBlockEntity.Entities);
blockEntities.Add(newInsert);
cadBlockEntity.Entities = blockEntities.ToArray();
// update size if some new entities extend it
cadImage.UpdateSize();
cadImage.Save(outPath, pdfOptions);
}
@hardp,
could you provide the example of the initial file? What is the target format? How to identify the existing block?
@oleksii.gorokhovatskyi Export to Pdf.Insert a block in the picture.
block.png (9.4 KB)
sample.zip (24.7 KB)
@hardp,
please, test this sample:
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
CadRasterizationOptions options = new CadRasterizationOptions();
options.Layouts = new string[] { "Model" };
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.VectorRasterizationOptions = options;
// search for available handle
string newObjectID = "FFFFFF";
if (cadImage.Header.HeaderProperties.ContainsKey(CadHeaderAttribute.HANDSEED))
{
newObjectID = ((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value;
int nextAvailableID = int.Parse(newObjectID, System.Globalization.NumberStyles.HexNumber) + 1;
((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value = nextAvailableID.ToString("X");
}
string blockName = "_CPPETJ角图章";
// new insert object
CadInsertObject newInsert = new CadInsertObject();
newInsert.Name = blockName;
newInsert.ObjectHandle = newObjectID;
newInsert.LayerName = "0";
newInsert.InsertionPoint.X = 100;
newInsert.InsertionPoint.Y = 100;
newInsert.ScaleX = 2;
// add new insert to Entities
List<CadEntityBase> entities = new List<CadEntityBase>(cadImage.Entities);
entities.Add(newInsert);
cadImage.Entities = entities.ToArray();
// search for Model block and add new insert there too
CadBlockTableObject blockTableObjectReference = null;
CadBlockEntity cadBlockEntity = null;
foreach (CadBlockTableObject tableObject in cadImage.BlocksTables)
{
if (string.Equals(tableObject.HardPointerToLayout, cadImage.Layouts["Model"].ObjectHandle))
{
blockTableObjectReference = tableObject;
break;
}
}
if (blockTableObjectReference != null
&& cadImage.BlockEntities.ContainsKey(blockTableObjectReference.BlockName))
{
cadBlockEntity = cadImage.BlockEntities[blockTableObjectReference.BlockName];
}
List<CadEntityBase> blockEntities = new List<CadEntityBase>(cadBlockEntity.Entities);
blockEntities.Add(newInsert);
cadBlockEntity.Entities = blockEntities.ToArray();
// update size if some new entities extend it
cadImage.UpdateSize();
cadImage.Save(outPath, pdfOptions);
}