可以获取当前图形的范围和背景图的范围来计算比例及插入点
块(背景图)的图形范围可通过getGeomExtents()一次取得,而当前图形的图形范围好像没什么函数、系统变量可直接取得。我想可以通过遍历所有实体,然后对所有实体getGeomExtents()来求出,也许可以先"zoom e" 然后取Viewport的范围来求得. 也许还有更高效的方法
给出一段VBA代码供参考, 转ARX应不难:

- ' 将外部图形文件尽可能大的插入到指定的范围
- Private Sub InsertXrefIn(XrefName As String, XrefFile As String, left As Double, bottom As Double, right As Double, top As Double)
- Dim InsertPoint(0 To 2) As Double
- Dim insertedBlock As AcadExternalReference
- Dim minExt As Variant
- Dim maxExt As Variant
-
- Dim dLeft As Double
- Dim dBottom As Double
- Dim dRight As Double
- Dim dTop As Double
-
- Dim dXScale As Double
- Dim dYscale As Double
-
- ThisDrawing.MSpace = False
-
- InsertPoint(0) = 0: InsertPoint(1) = 0: InsertPoint(2) = 0
-
- ' 插入到PaperSpace
- Set insertedBlock = ThisDrawing.PaperSpace.AttachExternalReference(XrefFile, XrefName, InsertPoint, 1, 1, 1, 0, False)
- ' 取块范围
- insertedBlock.GetBoundingBox minExt, maxExt
-
- dLeft = minExt(0)
- dBottom = minExt(1)
- dRight = maxExt(0)
- dTop = maxExt(1)
-
- dXScale = (right - left) / (dRight - dLeft)
- dYscale = (top - bottom) / (dTop - dBottom)
-
- If dXScale > dYscale Then
- dXScale = dYscale
- Else
- dYscale = dXScale
- End If
-
- InsertPoint(0) = (left + (right - left) / 2) - (dLeft + (dRight - dLeft) / 2) * dXScale
- InsertPoint(1) = (bottom + (top - bottom) / 2) - (dBottom + (dTop - dBottom) / 2) * dYscale
-
- insertedBlock.InsertionPoint = InsertPoint
- insertedBlock.XScaleFactor = dXScale
- insertedBlock.YScaleFactor = dYscale
-
- insertedBlock.Update
-
- End Sub
|