Python

Python Projects using PointElementInfo

Python projects Export Text, Import Text and Replace Text use the PointElementInfo class.

Explanation of Fields

The raison d'être for PointElementInfo is to record the details of a point element. For example, we may want to export text to Excel, or import cells from Excel and replace existing DGN objects.

Export

When we export from MicroStation …

Sometimes we look inside a complex element, such as a text node or a cell. In that case we record additionally …

Import

When we import to MicroStation …

Replace

When we replace existing text in MicroStation …

Python Implementation

class PointElementInfo (NamedTuple):
    ''' Class stores data about a DGN point element, such as a cell or text.
    The text stores the cell name or, if a text element, the text string it contains''' 
    elementId: int64
    levelId: int32
    text: str
    x: float
    y: float
    z: float
    source: str
    sourceId: int64

    def __str__(self):
        #   Override the default __str__ function to print the data in a text instance
        return f"'{self.text}', {str(self.elementId)}, {str(self.levelId)}, "
        	"{str(self.x)}, {str(self.y)}, {str(self.z)},"
        	" '{self.source}', {str(self.sourceId)}"

    def __repr__(self):
        #   Override the default __repr__ function to return a string representation of the instance in a format that can be used to recreate the object
        return f"PointElementInfo({str(self.elementId)}, {str(self.levelId)}, '{self.text}', "
        	"{str(self.x)}, {str(self.y)}, {str(self.z)}, "
        	"'{self.source}', {str(self.sourceId)})"

    def __gt__(self, other):
        # Provide a comparison > operator so that lists of this class can be sorted.
        # I chose to sort by the text content, but you might want some other criterion
        if isinstance(other, PointElementInfo):
        	# Compare two strings ignoring case using casefold()
            return self.text.casefold() > other.text.casefold()
        else:
            return NotImplemented

    def __eq__(self, other):
        # Provide an equality == operator.
        # I chose to compare the Element IDs, but you might want some other criterion
        if isinstance(other, PointElementInfo):
            return self.elementId == other.elementId
        else:
            return NotImplemented

    # Class method supplies a list of column names used, for example, in an Excel worksheet
    @classmethod
    # A class method is available anywhere.  Here we supply a list of column names for Excel
    def ColumnNames (cls)->list[str]:
       return ['Element ID', 'Level ID', 'Text', 'X', 'Y', 'Z', 'Source', 'Source ID']

PointElementInfo inherits from Python NamedTuple.

Note that Python provides two named tuple types...

We use the first of those in the PointElementInfo class.

PointElementInfo overrides Python magic methods __str__ and __repr__. You use those when you write something like this …

	pointInfo = PointElementInfo(1234, 5678, "word", 10.10, 11.11, 12.12, 'cell', 9876)
	pointInfo        # Calls __repr__
	print(pointInfo) # Calls __str__

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.