You're welcome.
I guess I'll correct a couple of, I think, inconsequential items in my earlier code.
in PolyLine2Connector, there is no need for MinDistance to be an array, just use MinDistance as a scalar wherever MinDistance() appears.
That probably doesn't matter at all, but in GlueCoords, I am needlessly using ReDim for each g, when we know the needed array size in advance
Code: Select all
Function GlueCoords(oShape As Object)
Dim ShapeCenter As New com.sun.star.awt.Point
Dim oGlue
Dim g As Long
Dim oPos As New com.sun.star.awt.Point
Dim oSize As New com.sun.star.awt.Size
Dim oPoint As New com.sun.star.drawing.GluePoint2
oPos = oShape.getPosition()
oSize = oShape.getSize()
ShapeCenter.X = oPos.X + oSize.Width\2
ShapeCenter.Y = oPos.Y + oSize.Height\2
oGlue = oShape.getGluePoints()
Dim GluePointCoords(oGlue.getCount() - 1) As New com.sun.star.awt.Point
For g = 0 to oGlue.getCount() - 1
oPoint = oGlue.getByIndex(g)
If oPoint.IsRelative Then
GluePointCoords(g).X = oPoint.Position.X
GluePointCoords(g).Y = oPoint.Position.Y
Else
GluePointCoords(g).X = ShapeCenter.X + oPoint.Position.X
GluePointCoords(g).Y = ShapeCenter.Y + oPoint.Position.Y
End If
Next g
GlueCoords = GluePointCoords
End Function
That probalby doesn't matter much either, but i can see where it might improve speed and allocation efficiency if there are many shapes.
Another thing I have been looking at is the business with PolyLines. In Your example that's mostly what we're dealing with, but the line going to the left from the Control Valve is a simple LineShape, and since its left terminus (with the arrow), is on a GroupShape, it is easy to include such a LineShape in the code
Code: Select all
Sub Line2Connector
Dim oDoc As Object
Dim oConnector As Object
Dim oShapes(0) As Object, oShape As Object
Dim oDP As Object
Dim oSelection As Object, oLine As Object
Dim i As Long, j As Long, k As Long, n As Long
Dim oCoords, PolyPoints
Dim EndPoints(1) As New com.sun.star.awt.Point
Dim MinDistance As Double, MinShapeIndex(1) As Long, MinGlueIndex(1) As Long
Dim Dist As Double
oDoc = ThisComponent
oSelection = oDoc.getCurrentController().getSelection()
oLine = oSelection.getByIndex(0)
'Make sure selection is a PolyLine or a line.
If oLine.getShapeType() <> "com.sun.star.drawing.PolyLineShape" And oLine.getShapeType() <> "com.sun.star.drawing.LineShape" Then
MsgBox("No Line Selected")
Exit Sub
End If
'Get EndPoints of the polyline
PolyPoints = oLine.getPropertyValue("Polygon")
EndPoints(0).X = PolyPoints(0).X
EndPoints(0).Y = PolyPoints(0).Y
EndPoints(1).X = PolyPoints(UBound(PolyPoints)).X
EndPoints(1).Y = PolyPoints(UBound(PolyPoints)).Y
oDP = oDoc.getDrawPages().getByIndex(0)
'Make a connector
oConnector = oDoc.createInstance("com.sun.star.drawing.ConnectorShape")
oConnector.EdgeKind = com.sun.star.drawing.ConnectorType.STANDARD
i = 0
n = 0
'Get all the GroupShapes
Do While i < oDP.getCount()
oShape = oDP.getByIndex(i)
If oShape.supportsService("com.sun.star.drawing.GroupShape") Then
Redim Preserve oShapes(n) As Object
oShapes(n) = oShape
n = n + 1
EndIf
i = i + 1
Loop
'Find closest GluePoint to each EndPoint
For i = 0 To 1
oCoords = GlueCoords(oShapes(0))
MinShapeIndex(i) = 0
MinGlueIndex(i) = 0
MinDistance = Distance(EndPoints(i),oCoords(0))
For j = 0 To n - 1
oCoords = GlueCoords(oShapes(j))
For k = 0 To UBound(oCoords)
Dist = Distance(EndPoints(i),oCoords(k))
If Dist < MinDistance Then
MinDistance = Dist
MinShapeIndex(i) = j
MinGlueIndex(i) = k
End If
Next k
Next j
Next i
'Connect the connector to the closest Gluepoints
oDP.add(oConnector)
oConnector.setPropertyValue("StartShape",oShapes(MinShapeIndex(0)))
oConnector.setPropertyValue("StartGluePointIndex",MinGlueIndex(0))
oConnector.setPropertyValue("EndShape",oShapes(MinShapeIndex(1)))
oConnector.setPropertyValue("EndGluePointIndex",MinGlueIndex(1))
'Remove original line
oDP.remove(oLine)
End Sub
As I supposed though, we'll run into difficulties. The line going left from the Pressure Gauge doesn't end at a Gluepoint, it just bumps into a vertical line. So if the macro is run on the line going left, it winds up connected to the wrong thing.
Another glitch was when I ran it on the PolyLine going into the top of the Vacuum Pump, since the PolyLine extended left, it wound up attaching to the left side of the pump instead of the top. I want to mention these things because I imagine you'll run into many such problems.
I also will say, though I'm sure you realize it, that someone very knowledgeable about the engineering better look at these things after the changes.