Sometimes can't get the IDs of connected shapes

I’m trying to get the IDs of the shapes connected to dynamic connectors and i’m using this code snippet to do that.

    val shapes = diagram.pages[0].shapes[0].shapes

        val connectionsID = mutableListOf<Long>()
        for (shapeIndex in 0 until shapes.count) {
            val currentShape = shapes[shapeIndex]
            if (currentShape.master.name.replace(Regex("[^a-zA-Z\\s]"), "") == "Dynamic connector") {
                connectionsIDs.add(currentShape.connectorRule.startShapeId )
                connectionsIDs.add(currentShape.connectorRule.endShapeId )
            }
        }

this way returns a list of Ids which is supposed to be the IDs of the shapes connected to the current shape

but when i try to get the connected shapes objects again using the IDs i retrieved above i get back NULL shape

connectorsIDs.forEach { connectorData ->
            val shape = shapes.getShape(connectorData.startShapeId)       
}

so as described the shape returned with these methods is NULL even though I am using the IDs from the list i got from the shapes connected to the dynamic connectors.
Note: This happens with most of the files i am working on but rarely it works on minority of the files without any hints of the reason why.

@Mohamed_Elshehawy
Could you please share the sample file and share your current code that you are using. We will check and assist you accordingly.
Thanks.

VisioProject.zip (589.7 KB)

class ManipulateBoard() {
    private lateinit var shapesData: List<ShapeAttributes>
    val connectorMaster = "Dynamic connector"


    fun extractShapesData(
        page: Page,
        diagram: Diagram
    ) {
        val shapes = page.shapes[0].shapes

        val connectionsData = mutableListOf<ConnectorData>()
        for (shapeIndex in 0 until shapes.count) {
            val currentShape = shapes[shapeIndex]
            if (currentShape.name.replace(Regex("[^a-zA-Z\\s]"), "") == "Dynamic connector") {
                connectionsData.add(
                    ConnectorData(
                        startShapeId = currentShape.connectorRule.startShapeId,
                        endShapeId = currentShape.connectorRule.endShapeId
                    )
                )

            }
        }
        repositionShapesOnBoard(page)
        removeConnectors(shapes)
        reconnectShapesAgain(diagram = diagram, page = page, connectorsData = connectionsData)
    }


    private fun removeConnectors(shapes: ShapeCollection) {
        val shapesToDelete = mutableListOf<Shape>()
        for (shapeIndex in 0 until shapes.count) {
            val currentShape = shapes[shapeIndex]
            if (currentShape.name.replace(Regex("[^a-zA-Z\\s]"), "").contains("Dynamic connector")) {
                shapesToDelete.add(currentShape)
            }
        }

        shapesToDelete.forEach { shape ->
            shapes.remove(shape)
        }
    }

    private fun repositionShapesOnBoard(board: Page) {
        val boardWidth = board.pageSheet.pageProps.pageWidth.value
        val shapes = board.shapes[0].shapes
        for (shapeIndex in 0 until shapes.count) {
            val currentShape = shapes[shapeIndex]
            currentShape.name.let { name ->
                val newName = currentShape.name.replace(Regex("[^a-zA-Z\\s]"), "")
                println("shape name is $newName")
                if (newName != "Swimlane"
                    && newName != "Swimlane List"
                    && newName != "Phase List"
                    && newName != "Separator"
                    && newName != "CFF Container"
                    && newName != "Dynamic connector"
                ) {
                    val newXPosition = boardWidth - currentShape.xForm.pinX.value
                    currentShape.moveTo(newXPosition, currentShape.xForm.pinY.value)
                }
            }
        }

    }


    private fun reconnectShapesAgain(page: Page, diagram: Diagram, connectorsData: List<ConnectorData>) {
        val shapes = page.shapes[0].shapes

        connectorsData.forEach { connectorData ->
            val connector = Shape()
            val connectorId: Long = diagram.addShape(connector, connectorMaster, 0)
            val firstShape = shapes.getShape(connectorData.startShapeId)
            val secondShape = shapes.getShape(connectorData.endShapeId)
            val firstShapeX = firstShape.xForm.pinX.value
            val firstShapeY = firstShape.xForm.pinY.value
            val secondShapeX = secondShape.xForm.pinX.value
            val secondShapeY = secondShape.xForm.pinY.value

            val xAxisDiff = abs(firstShapeX - secondShapeX)
            val yAxisDiff = abs(firstShapeY - secondShapeY)

            val pointInFirstShape = if (xAxisDiff > yAxisDiff) {
                if (firstShapeX > secondShapeX) {
                    ConnectionPointPlace.LEFT
                } else {
                    ConnectionPointPlace.RIGHT
                }
            } else {
                if (firstShapeY > secondShapeY) {
                    ConnectionPointPlace.BOTTOM
                } else {
                    ConnectionPointPlace.TOP
                }
            }

            val pointInSecondShape = when (pointInFirstShape) {
                ConnectionPointPlace.LEFT -> {
                    ConnectionPointPlace.RIGHT
                }

                ConnectionPointPlace.RIGHT -> {
                    ConnectionPointPlace.LEFT
                }

                ConnectionPointPlace.TOP -> {
                    ConnectionPointPlace.BOTTOM
                }

                else -> {
                    ConnectionPointPlace.TOP
                }
            }

            page.connectShapesViaConnector(
                connectorData.startShapeId,
                pointInFirstShape,
                connectorData.endShapeId,
                pointInSecondShape,
                connectorId
            )
        }
    }

this is All the code.
simply for my current project I’m trying to move the shapes to a new destination (Flip them horizontally) then I’m removing dynamic connectors because they make the drawing messy after I change the position of the shapes, so I remove the connectors while storing the ids of the shapes connected to them.
then using my logic, I start redrawing the connectors again but after making some calculation to make the connectors looks tidy.
then i get the problem mentioned above when trying to get the shapes that was connected to connectors by IDs.

@Mohamed_Elshehawy
Thank you for providing the sample code. We need to convert this code into Java.
We have created a new ticket to track this issue.
Thanks.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): DIAGRAMJAVA-51196

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.