The issue that parse a table in detail

Hi, I want to use cad java to parse some most basic, but the most important properties about a table, Including
1.the most basic table layout (A cell may be merged from multiple cells)
2. the whole position of table and every position of cell
3. the text info in every cell.

I think above info can build a basic table to show the correct information
please give me a whole example to this.

Thank you so much

@jim_zhu

To parse a table from a DWG or DXF file using Aspose.CAD for Java, you can follow the steps outlined below. This example will help you retrieve the basic properties of a table, including the layout, positions of the table and cells, and the text information in each cell.

Example Code

import com.aspose.cad.Image;
import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.fileformats.cad.entities.CadBlockEntity;
import com.aspose.cad.fileformats.cad.entities.CadEntityTypeName;
import com.aspose.cad.fileformats.cad.entities.CadMText;
import com.aspose.cad.fileformats.cad.entities.CadText;

import java.util.ArrayList;
import java.util.List;

public class ParseCadTable {
    public static void main(String[] args) {
        // Load the CAD file
        String fileName = "path/to/your/file.dwg"; // Update with your file path
        CadImage cadImage = (CadImage) Image.load(fileName);

        // List to hold table blocks
        List<CadBlockEntity> tableBlocks = new ArrayList<>();

        // Retrieve block entities
        var entities = cadImage.getBlockEntities().getValuesTyped();
        for (CadBlockEntity entity : entities) {
            // Check if the entity is a table (usually starts with "*T")
            if (entity.getName().startsWith("*T")) {
                tableBlocks.add(entity);
            }
        }

        // Iterate through the found table blocks
        for (CadBlockEntity tableBlock : tableBlocks) {
            System.out.println("Table Block Name: " + tableBlock.getName());
            var textElements = tableBlock.getEntities();
            for (var entity : textElements) {
                // Check for text entities
                if (entity.getTypeName() == CadEntityTypeName.TEXT) {
                    CadText textEntity = (CadText) entity;
                    System.out.println("Cell Text: " + textEntity.getDefaultValue());
                } else if (entity.getTypeName() == CadEntityTypeName.MTEXT) {
                    CadMText mTextEntity = (CadMText) entity;
                    System.out.println("Cell MText: " + mTextEntity.getFullClearText());
                }
            }
        }
    }
}

Explanation

  1. Loading the CAD File: The code begins by loading a DWG or DXF file using the Image.load method.
  2. Retrieving Table Blocks: It iterates through the block entities in the CAD file, checking for those that represent tables (identified by names starting with “*T”).
  3. Extracting Text Information: For each table block, it retrieves the text elements and prints the text contained in each cell, distinguishing between single-line text (CadText) and multi-line text (CadMText).

Additional Information

  • Ensure you have the Aspose.CAD library included in your project to run this code.
  • Modify the file path to point to your specific DWG or DXF file.

This example should provide a solid foundation for parsing tables in CAD files and extracting the necessary information you need. If you have further questions or need additional features, feel free to ask!

Sources:
[1]: Reading of tables from DWG/DXF|CAD Documentation

@jim_zhu,
Hello,
table is a complex entity, and stores data in different structures, sometimes with duplication of values. Please test if example below is useful:

final CadImage cadImage = (CadImage)Image.load(inputFile);

for (CadEntityBase baseEntity: cadImage.getEntities())
{
	if (baseEntity.getTypeName() == CadEntityTypeName.ACAD_TABLE)
	{
		CadTableEntity table = (CadTableEntity)baseEntity;

		// the way to get information from the entities that form the table
		CadBlockEntity block = cadImage.getBlockEntities().get_Item(table.getBlockName());

		for (CadEntityBase blockEntity: block.getEntities())
		{
			if (blockEntity.getTypeName() == CadEntityTypeName.MTEXT)
			{
				CadMText mtext = (CadMText)blockEntity;
				System.out.println(String.format("Text = %s", mtext.getText() ));

				Cad3DPoint locationpoint = mtext.getInsertionPoint();
				System.out.println(String.format("Position = (%f, %f)", locationpoint.getX(), locationpoint.getY()));
			}
		}

		Cad3DPoint tableLocation = table.getInsertionPoint();
		System.out.println(String.format("Table location = (%f, %f)",
				tableLocation.getX(), tableLocation.getY()));

		CadFormattedTableData formattedData = table.getFormattedTableData();

		for (FormattedTableCellRange mergedCells: formattedData.getMergedCellRanges())
		{
			System.out.println(String.format("Merged cell from row %d to row %d and from col %d to col %d",
					mergedCells.getBottomRowIndex(), mergedCells.getTopRowIndex(),
					mergedCells.getLeftColumnIndex(), mergedCells.getRightColumnIndex()));
		}

		CadLinkedTableData linkedTableData = table.getLinkedTableData();
		System.out.println(String.format("Columns: %d, rows: %d",
				linkedTableData.getColumns().size(), linkedTableData.getRows().size()));

		for (TableDataColumn col: linkedTableData.getColumns())
		{
			System.out.println("Width of column = " + col.getWidth());
		}

		for (TableDataRow row: linkedTableData.getRows())
		{
			for (TableDataCell cell: row.getCells())
			{
				for (TableDataCellContent content: cell.getCellContents())
				{
					System.out.println(content.getFieldValue().getAttribute001());
				}
			 }
		}
	}
}

Hello,

Thank you for your previous help, meanwhile, I encounter some questions during parse a table whose ‘break’ property is ‘true’.

From result, this one table will be figured out to several table objects. the first object has the full data (which I call it main table object) , and the other objects have the data belongs to their respective breaking part (which I call it additional table object).

My Questions are

If I got a table object,

  1. How could I know if this table have been broken ,or not?
  2. How should I know which object is the main object, which objects are additional objects.
  3. How should I find the relationship between the main object and additional objects.Is there any property can indicate it?

Thank you.

@jim_zhu,
Hello.
We don’t store the flag that shows that the table is broken, we have planned this in the scope of CADJAVA-11969. But parts of the broken table are stored as separate entities in the file with the relation in additional special objects. Please find the example below that shows how to find the handle of the second part of the table from the main part.

CadTableEntity table = (CadTableEntity)baseEntity;

List<CadBaseObject> allRelatedObjects = FindObjectsReferringTo(cadImage, table.getObjectHandle());

for (CadBaseObject baseObject: allRelatedObjects)
{
	if (baseObject.getTypeName() != CadObjectTypeName.DICTIONARY) continue;

	String xrecordHandle = ((CadDictionary)baseObject).getEntrySoftOwners().get(0).getValue();
	CadXRecord xrecord = (CadXRecord)FindObject(cadImage, xrecordHandle);

	for (CadCodeValue codeValue : xrecord.getObjects())
	{
		// code 102 should be ACAD_ROUNDTRIP_2008_TABLE_ENTITY
		// code 330 points to the ObjectHandle of the entity with second part of the table 
		System.out.println(codeValue.getCode() + " " + codeValue.getValue());
	}
}

private static List<CadBaseObject> FindObjectsReferringTo(CadImage cadImage, String handle)
{
	List<CadBaseObject> objects = new ArrayList<CadBaseObject>();

	for (CadBaseObject bo : cadImage.getObjects())
	{
		String softOwner = bo.getSoftOwner();
		if (softOwner != null && softOwner.equals(handle))
		{
		   objects.add(bo);
		}
	}

	return objects;
}
	
	
private static CadBaseObject FindObject(CadImage cadImage, String handle)
{
	for (CadBaseObject bo : cadImage.getObjects())
	{
		if (bo.getObjectHandle().equals(handle))
		{
			return bo;
		}
	}

	return null;
}