目录

Revit二次开发之移动元素比目鱼原创

目录

Revit二次开发之移动元素【比目鱼原创】

API提供了移动元素的方法,可以把一个或者几个元素从一个地方移动到另一个地方,

Revit API提供ElementTransformUtils类下的

MoveElement(Document document, ElementId elementToMove, XYZ translation)

以及MoveElements(Document document, ICollection elementsToMove, XYZ translation)

使用起来比较简单, 但是有几个需要注意的地方

移动方法不能远离标高向上或者向下移动一个基于标髙的元素。也就是说,当元素是基于标髙的,则不能改变Z轴坐标值,但可以移动元素到同一标高内的任意位置。例如, 如果在坐标点(0, 0, 0)新创建了一个柱子,然后移动它到新的坐标点(10, 20, 30),这个柱 子将会移动到(10, 20, 0),而不是(10, 20, 30)。

当移动一个元素的时候,其他的元素也许会跟着移动。例如,如果—个墙上有窗户,这个墙移动了,窗户也会跟着移动。移动元素集的方法也会出现这种情况。例如,移动几根柱子的时候,所有与柱子连接着的梁也会跟着被移动或者会被改变长度。

如果元素被钉住,即Pinned属性返回值是true,则表明这个元素不能被移动。如果仍然使用MoveEIement方法来移动这个元素,API就会抛出InvalidOperationException提示用户不能移动被钉住的元素。

只外,还可以通过Location类来移动一个元素。Location类提供了移动和旋转的方法,而其子类提供了更多的Location信息和属性控制,如LocationPoint类和 locationCurve类。如果一个元素的Location可以转型为LocationCurve或者 LocationPoint,就可以直接移动这根线或者点到新的坐标点了。

Wall wall = element as Wall; 
if (null != wall) 
{ 
    LocationCurve wallLine = wall.Location as LocationCurve; 
    XYZ newPlace = new XYZ(10, 20, 0); 
    wallLine.Move(newPlace); 
} 

上面的方法中,向量 (10, 20, 0)并不是目标坐标值, 而是一个偏移向量。

另外,LocationCurve 的 Curve 属性或者 LocationPoint 的Point属性也可以用来移动一个元素。

Curve属性可以用来移动基于曲线的元素到精确的坐标点。 墙、梁、支撑等都通常用这个属性改变元素的长度。

示例代码:

using(Transaction  tran  =  new  Transaction(projectDoc,  "Change  the  wall's  curve  with  a  new 
location line.")) 
{ 
  tran.Start(); 
                               
  LocationCurve wallLine = wall.Location as LocationCurve; 
  XYZ p1 = XYZ.Zero; 
  XYZ p2 = new XYZ(10, 20, 0); 
  Line newWallLine = Line.CreateBound(p1, p2); 
  // 把墙的位置线换成新的线 
  wallLine.Curve = newWallLine; 
                         
  tran.Commit(); 
} 

利用LocationPoint的Point属性,可以设置元素的物理位置,如:

FamilyInstance column = element as FamilyInstance; 
if (null != column) 
{ 
    LocationPoint columnPoint = column.Location as LocationPoint; 
    XYZ newLocation = new XYZ(10, 20, 0); 
    // 移动柱子到新的位置(10,20,0) 
    columnPoint.Point = newLocation; 
}

=========【

更多高级应用请关注公众号

】========

https://img-blog.csdn.net/20171215213618491?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmJreHcwMDE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

===================================