Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Vector problem in CopyElements

lavicka.milan
Contributor

Vector problem in CopyElements

lavicka.milan
Contributor
Contributor

hi i'm new in Revit API. I am trying to copy rebars from one wall to another above. It works fine but I have a problem with vector in Copy Elements methods. I thing it gives me normalized vector and i can't thing how to get vector as point without normalize function.

 def _rebars(self):
        rebars = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rebar).\
                WhereElementIsNotElementType()
        rebarlist = list()
        for rebar in rebars:
            rebar_host_id = rebar.GetHostId()
            if rebar_host_id == self.obj1.Id:
                rebarlist.append(rebar.Id)
                self.itemlist = List[ElementId](rebarlist)
                            
    
    def _vector(self):
        point_obj1 = self.obj1.Location.Curve.GetEndPoint(0)
        point_obj2 = self.obj2.Location.Curve.GetEndPoint(0)
        self.line = Line.CreateUnbound(point_obj1,point_obj2).Direction
        self.line2 = (point_obj1-point_obj2).Normalize()
        print(point_obj1,point_obj2)
        print(self.line)
        print(self.line2)

    def _rcopy(self):
        self._rebars()
        self._vector()
        t = Transaction(doc,"Copy Rebars")
        t.Start()
        newrebars = ElementTransformUtils.CopyElements(doc,self.itemlist,self.line)
        t.Commit()

_vector OUTPUT.JPG

0 Likes
Reply
Accepted solutions (1)
444 Views
3 Replies
Replies (3)

jeremytammik
Autodesk
Autodesk
Accepted solution

The overload of CopyElements that you are using takes a simple XYZ object argument to define the translation vector:

 

https://www.revitapidocs.com/2020/0e533605-477f-dd92-2376-15ff7cd4411c.htm

 

You can obtain that simply by subtracting the source point from the target point:

 

  XYZ translation_vector 
    = target_element.Location.Curve.GetEndPoint(0)
      - source_element.Location.Curve.GetEndPoint(0)

  

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

lavicka.milan
Contributor
Contributor

thanks it helps.

I have a one more question. Is there a way to make sure a value of XYZtransaction_vector.Z is always positive? I can't manipulate it like a list. 

0 Likes

jeremytammik
Autodesk
Autodesk

I don't quite understand why you ask, but of course you can always simply say something like this:

 

  XYZ v = a vector of some kind
  if 0 < v.Z:
    do something sensible

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder