Copy with base point

Anonymous

Copy with base point

Anonymous
Not applicable

Hi,

 

How do I call copy with base point via api.

 

Thanks & Regards

Sanjay Pandey(BIM[Revit] Developer)

0 Likes
Reply
Accepted solutions (1)
1,733 Views
18 Replies
Replies (18)

stever66
Advisor
Advisor

Something like this:

 

Selection selection = uidoc.Selection;

ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

 

XYZ point1 = new XYZ(0, 0, 0);

XYZ point2 = new XYZ(0, 0, 0);

XYZ point3 = new XYZ(0, 0, 0);

 

point1 = selection.PickPoint("Please pick a base point.");

point2 = selection.PickPoint("Please pick a point to place elements");

point3 = point2.Subtract(point1);

t.Start();

ICollection<ElementId> pastedIds = ElementTransformUtils.CopyElements(doc, selectedIds, point3);

t.Commit();

 

The view must have a workplane set for the PickPoint to work.  With a workplane set, this will work in plan views, elevation views, and even 3D views.

 

 

Mustafa.Salaheldin
Collaborator
Collaborator

I can't understand the question, can you clarify more please?


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes

Anonymous
Not applicable

I have a group whose group origin i have changed. Now How do I find the center point of the group. because after changing the group origin to the top left the rotation of the group is not working properly.

 

I have a group whose group origin i have changed. Now How do I find the center point of the group.

 

Thanks & Regards

Sanjay Pandey(BIM[Revit] Developer)

0 Likes

Revitalizer
Advisor
Advisor

Hi,

 

for the group's center point, why not get the bounding boxes of the members and then calculate it ?

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes

Anonymous
Not applicable

Hi,

 

Thanks for the suggestion.

 

How about getting the bounding box of the group itself and getting the center.

 

Thanks & Regards

Sanjay Pandey

0 Likes

Revitalizer
Advisor
Advisor

Hi sanjayman,

 

I've already seen that you created another thread for the Group center point.

http://forums.autodesk.com/t5/revit-api/group-center-point/td-p/6379898

 

As far as I can see, there are about three types of Groups:

 

  1. Model groups
  2. Detail groups
  3. Model groups with attached Detail groups

Each group consists of a list of members.

You can get the members' BoundingBoxes, this method needs a View parameter.

Depending on the kind of the group,use the View the Element resides in (Detail group) or null (for Model group).

May be that you need to use a valid View3D instead of null, in this case.

 

For the third case, I have no idea so far.

 

Once having the BoundinBoxXYZ for each Element in a group, get their minimum and maximum extends.

Calculate the center of the extends by dividing it by two...

 

(Remark: Groups can contain groups.)

 

That's all.

 

 

Revitalizer

 

 




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes

Anonymous
Not applicable

Hi,

 

I am only dealing with Model Groups[no detail group attached nor any group within group]. After replying to you I tried to do :

 

1. got the bounding box of the group group.get_BoundingBox(activeview)

2. divide the max and min by two.

 

but results are quiet badSmiley Frustrated

 

Thanks & Regards

Sanjay Pandey(BIM[Revit] Developer)

0 Likes

Revitalizer
Advisor
Advisor

Hi,

 

not the BoundingBox of the Group itself but those boxes of its members.

Get the member Elements (respectively their Ids) by Group.GetMemberIds().

 

Note that Groups can be embedded recursively, a Group's member could be another Group, since a Group is an Element itself.

 

For a Model group, I wouldn't use the ActiveView since the result will be view depending.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes

Mustafa.Salaheldin
Collaborator
Collaborator

Please try this and tell me the result:

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS5
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                FilteredElementCollector collector = new FilteredElementCollector(CachedDoc);
                collector.OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType();

                List<Element> els = collector.Cast<Element>().ToList();

                Element el = els.First();
                Group g = el as Group;

                BoundingBoxXYZ box = g.get_BoundingBox(CachedDoc.ActiveView);

                XYZ p1 = box.Max.Add(box.Min) / 2;
                XYZ p2 = p1.Add(new XYZ(0, 0, 1));

                Line ln = Line.CreateBound(p1, p2);

                using (Transaction t = new Transaction(CachedDoc, "Revit"))
                {
                    t.Start();

                    ElementTransformUtils.RotateElement(CachedDoc, el.Id, ln, Math.PI / 2);

                    t.Commit();
                }

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes

stever66
Advisor
Advisor

@Anonymous wrote:

Hi,

 

I am only dealing with Model Groups[no detail group attached nor any group within group]. After replying to you I tried to do :

 

1. got the bounding box of the group group.get_BoundingBox(activeview)

2. divide the max and min by two.

 

but results are quiet badSmiley Frustrated

 

Thanks & Regards

Sanjay Pandey(BIM[Revit] Developer)


For step 2, don't you mean:

 

min + (max - min)/2  ?

 

 

0 Likes

Mustafa.Salaheldin
Collaborator
Collaborator

Please try this and tell me the result:

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS5
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                FilteredElementCollector collector = new FilteredElementCollector(CachedDoc);
                collector.OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType();

                List<Element> els = collector.Cast<Element>().ToList();

                Element el = els.First();
                Group g = el as Group;

                BoundingBoxXYZ box = g.get_BoundingBox(CachedDoc.ActiveView);

                XYZ p1 = box.Max.Add(box.Min) / 2;
                XYZ p2 = p1.Add(new XYZ(0, 0, 1));

                Line ln = Line.CreateBound(p1, p2);

                using (Transaction t = new Transaction(CachedDoc, "Revit"))
                {
                    t.Start();

                    ElementTransformUtils.RotateElement(CachedDoc, el.Id, ln, Math.PI / 2);

                    t.Commit();
                }

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes

Anonymous
Not applicable

Dear Mustafa,

I don;t know but I see a bonding getting developed here.Smiley Wink Thanks for spending time for me. Can I have your emailid?

 

I tried your code and you can see the results in the image below.

mustafa.png

 

 

 

 

0 Likes

Mustafa.Salaheldin
Collaborator
Collaborator

You have to adjust the angle in the code because it is already set to 90 degree (Math.Pi / 2).

 

I've modified the code to make you able to control the value of the angle.

 

If this reply satisfies your need please don't forget to mark it as solution.

 This code will rotate the group by 180 dgree now.

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS5
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                FilteredElementCollector collector = new FilteredElementCollector(CachedDoc);
                collector.OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType();

                List<Element> els = collector.Cast<Element>().ToList();

                Element el = els.First();
                Group g = el as Group;

                BoundingBoxXYZ box = g.get_BoundingBox(CachedDoc.ActiveView);

                XYZ p1 = box.Max.Add(box.Min) / 2;
                XYZ p2 = p1.Add(new XYZ(0, 0, 1));

                Line ln = Line.CreateBound(p1, p2);

                using (Transaction t = new Transaction(CachedDoc, "Revit"))
                {
                    t.Start();

                    //control the rotation angle from here.
                    double RotationAngle = Math.PI;
                    ElementTransformUtils.RotateElement(CachedDoc, el.Id, ln, RotationAngle);

                    t.Commit();
                }

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

 

 

 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes

Anonymous
Not applicable

don;t know what's going wrong;

 

my code with your help goes like this:

 

trans.Start("sun");
XYZ targetPoint = Command.uidoc.Selection.PickPoint("Please pick a point.");

Group dt = uidoc.Document.Create.PlaceGroup(targetPoint, ty);

BoundingBoxXYZ box1 = dt.get_BoundingBox(Command.doc.ActiveView);
XYZ p1 = box1.Max.Add(box1.Min) / 2;
XYZ p2 = p1.Add(new XYZ(0, 0, 1));
Line ln = Line.CreateBound(p1, p2);

ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, 180);

 

result:

 

mustafa1.png

 

 

 

 

0 Likes

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Revit uses the radian measurments for angel so your code should be:

 

trans.Start("sun");
 XYZ targetPoint = Command.uidoc.Selection.PickPoint("Please pick a point.");

Group dt = uidoc.Document.Create.PlaceGroup(targetPoint, ty);

BoundingBoxXYZ box1 = dt.get_BoundingBox(Command.doc.ActiveView);
 XYZ p1 = box1.Max.Add(box1.Min) / 2;
 XYZ p2 = p1.Add(new XYZ(0, 0, 1));
 Line ln = Line.CreateBound(p1, p2);

// replace 180 with Math.Pi
ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, Math.Pi);

 

To rotate around the arbitary point

 

trans.Start("sun");
 XYZ targetPoint = Command.uidoc.Selection.PickPoint("Please pick a point.");

Group dt = uidoc.Document.Create.PlaceGroup(targetPoint, ty);

BoundingBoxXYZ box1 = dt.get_BoundingBox(Command.doc.ActiveView);
 XYZ p1 = targetPoint ;
 XYZ p2 = p1.Add(new XYZ(0, 0, 1));
 Line ln = Line.CreateBound(p1, p2);

ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, 180);

 

 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Anonymous
Not applicable

sorry but the revised code did nothing. In fact it even didn;t got rotated

0 Likes

Mustafa.Salaheldin
Collaborator
Collaborator

Sorry it was my mistake Smiley Sad

 

trans.Start("sun");
 XYZ targetPoint = Command.uidoc.Selection.PickPoint("Please pick a point.");

Group dt = uidoc.Document.Create.PlaceGroup(targetPoint, ty);

BoundingBoxXYZ box1 = dt.get_BoundingBox(Command.doc.ActiveView);
 XYZ p1 = targetPoint ;
 XYZ p2 = p1.Add(new XYZ(0, 0, 1));
 Line ln = Line.CreateBound(p1, p2);

ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, Math.PI);

 I forget to repalce the 180 by PI Smiley Very Happy

 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes

Anonymous
Not applicable

Dear Mustafa,

 

Thanks for you time and help. Your code did the trick only with a very very little minor change.

 

trans.Start("sun");
 XYZ targetPoint = Command.uidoc.Selection.PickPoint("Please pick a point.");

Group dt = uidoc.Document.Create.PlaceGroup(targetPoint, ty);

BoundingBoxXYZ box1 = dt.get_BoundingBox(Command.doc.ActiveView);
 XYZ p1 = box1.Max.Add(box1.Min) / 2;
 XYZ p2 = p1.Add(new XYZ(0, 0, 1));
 Line ln = Line.CreateBound(p1, p2);

// replace 180 with Math.Pi
//ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, Math.Pi); 
//Replaced above line with
ElementTransformUtils.RotateElement(Command.doc, dt.Id, ln, rotation defined bu user * Math.PI / 180);

 

Again a big thank for you. Kudos to you and I will accept your solution as accepted answer. But I want to carry it forward. I am posting a new query in the forum. I which I will be needing your expert suggestion.

 

Thanks & Regards

Sanjay Pandey(BIM[Revit] Developer)