private void SetDoorOffset(int id)
        {
            try
            {
                foreach (Tuple <Autodesk.Revit.DB.FamilyInstance, Autodesk.Revit.DB.Wall> curSelectedDoorAndWall in selectedDoorsAndWalls)
                {
                    Autodesk.Revit.DB.FamilyInstance selectedDoor = curSelectedDoorAndWall.Item1;
                    Autodesk.Revit.DB.Wall           selectedWall = curSelectedDoorAndWall.Item2;

                    if ((selectedDoor != null) && (selectedWall != null))
                    {
                        try
                        {
                            double selectedDoorWidth = selectedDoor.Symbol.get_Parameter(BuiltInParameter.DOOR_WIDTH).AsDouble();
                            double selectedWallWidth = selectedWall.Width;

                            LocationCurve selectedWallLocationCurve = selectedWall.Location as LocationCurve;
                            Curve         selectedWalCurve          = selectedWallLocationCurve.Curve;

                            Wall          hostWall = selectedDoor.Host as Wall;
                            LocationCurve hostWallLocationCurve = hostWall.Location as LocationCurve;
                            Curve         hostWallCurve         = hostWallLocationCurve.Curve;

                            IntersectionResultArray ira = new IntersectionResultArray();
                            SetComparisonResult     scr = selectedWalCurve.Intersect(hostWallCurve, out ira);

                            var iter = ira.GetEnumerator();
                            if (iter.MoveNext())
                            {
                                IntersectionResult ir = iter.Current as IntersectionResult;
                                if (ir != null)
                                {
                                    XYZ    intersectionXYZ   = ir.XYZPoint;
                                    double intersectionParam = hostWallCurve.Project(intersectionXYZ).Parameter;

                                    LocationPoint doorPoint = selectedDoor.Location as LocationPoint;
                                    XYZ           doorXYZ   = doorPoint.Point;
                                    double        doorParam = hostWallCurve.Project(doorXYZ).Parameter;

                                    XYZ translation           = null;
                                    XYZ doorEdgeXYZ           = null;
                                    XYZ intersectionOffsetXYZ = null;
                                    if (intersectionParam > doorParam)
                                    {
                                        intersectionOffsetXYZ = hostWallCurve.Evaluate(intersectionParam - this.selectedOffset - selectedWallWidth / 2, false);
                                        doorEdgeXYZ           = hostWallCurve.Evaluate(doorParam + selectedDoorWidth / 2, false);
                                        translation           = intersectionOffsetXYZ.Subtract(doorEdgeXYZ);
                                    }
                                    else
                                    {
                                        intersectionOffsetXYZ = hostWallCurve.Evaluate(intersectionParam + this.selectedOffset + selectedWallWidth / 2, false);
                                        doorEdgeXYZ           = hostWallCurve.Evaluate(doorParam - selectedDoorWidth / 2, false);
                                        translation           = doorEdgeXYZ.Subtract(intersectionOffsetXYZ).Negate();
                                    }

                                    ElementTransformUtils.MoveElement(doc, selectedDoor.Id, translation);
                                }
                            }
                        }
                        catch (Exception Exception)
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Exception", ex.Message);
            }
        }
示例#2
0
        public void DoorWallOffset()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            // establish an offset of 20cm that will be applied to the door and wall
            // Revit's internal units for distance is feet
            double doorToWallDistance = 20 / (12 * 2.54); // convert 20 cm to 1 ft

            using (InputForm myInputDisForm = new InputForm())
            {
                myInputDisForm.ShowDialog();

                //if the user hits cancel just drop out of macro
                if (myInputDisForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
                {
                    //else do all this :)
                    myInputDisForm.Close();
                }

                if (myInputDisForm.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    //else do all this :)
                    doorToWallDistance = Convert.ToDouble(myInputDisForm.distance_tb.Text) / (12 * 25.4);
                    myInputDisForm.Close();
                }
            }

            try // try/catch block with PickObject is used to give the user a way to get out of the infinite loop of while (true)
                // PickObject throws an exception when user aborts from selection
            {
                while (true)
                {
                    List <int> myListIdCategoryDoor = new List <int>();
                    myListIdCategoryDoor.Add((int)BuiltInCategory.OST_Doors);
                    myListIdCategoryDoor.Add((int)BuiltInCategory.OST_Windows);


                    FamilyInstance door = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, new FilterByIdCategory(myListIdCategoryDoor),
                                                                                    "Select a door or window. ESC when finished.").ElementId) as FamilyInstance;
                    double doorWidth = door.Symbol.get_Parameter(BuiltInParameter.DOOR_WIDTH).AsDouble();


                    List <int> myListIdCategoryWall = new List <int>();
                    myListIdCategoryWall.Add((int)BuiltInCategory.OST_Walls);

                    Wall sideWall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, new FilterByIdCategory(myListIdCategoryWall),
                                                                              "Select a wall. ESC when finished.").ElementId) as Wall;
                    double sideWallWidth = sideWall.Width;

                    // get the curve that defines the centerline of the side wall
                    LocationCurve sideWallLocationCurve = sideWall.Location as LocationCurve;
                    Curve         sideWallCurve         = sideWallLocationCurve.Curve;

                    // get the curve that defines the centerline of the wall that hosts the door
                    Wall          hostWall = door.Host as Wall;
                    LocationCurve hostWallLocationCurve = hostWall.Location as LocationCurve;
                    Curve         hostWallCurve         = hostWallLocationCurve.Curve;

                    // find the point of intersection of these two wall curves (intersectionXYZ)
                    // and the position of this point on the wall that hosts the door (intersectionParam)
                    IntersectionResultArray ira = new IntersectionResultArray();
                    SetComparisonResult     scr = sideWallCurve.Intersect(hostWallCurve, out ira);
                    var iter = ira.GetEnumerator();
                    iter.MoveNext();
                    IntersectionResult ir    = iter.Current as IntersectionResult;
                    XYZ    intersectionXYZ   = ir.XYZPoint;
                    double intersectionParam = hostWallCurve.Project(intersectionXYZ).Parameter;

                    // find the position of the door in its host wall
                    LocationPoint doorPoint = door.Location as LocationPoint;
                    XYZ           doorXYZ   = doorPoint.Point;
                    double        doorParam = hostWallCurve.Project(doorXYZ).Parameter;

                    // compute the translation vector between the edge of the door closest to the wall and the side face of the wall
                    XYZ translation           = null;
                    XYZ doorEdgeXYZ           = null;
                    XYZ intersectionOffsetXYZ = null;
                    if (intersectionParam > doorParam)
                    {
                        intersectionOffsetXYZ = hostWallCurve.Evaluate(intersectionParam - doorToWallDistance - sideWallWidth / 2, false);
                        doorEdgeXYZ           = hostWallCurve.Evaluate(doorParam + doorWidth / 2, false);
                        translation           = intersectionOffsetXYZ.Subtract(doorEdgeXYZ);
                    }
                    else
                    {
                        intersectionOffsetXYZ = hostWallCurve.Evaluate(intersectionParam + doorToWallDistance + sideWallWidth / 2, false);
                        doorEdgeXYZ           = hostWallCurve.Evaluate(doorParam - doorWidth / 2, false);
                        translation           = doorEdgeXYZ.Subtract(intersectionOffsetXYZ).Negate();
                    }

                    // Move the door
                    using (Transaction t = new Transaction(doc, "move door"))
                    {
                        t.Start();
                        ElementTransformUtils.MoveElement(doc, door.Id, translation);
                        t.Commit();
                    }
                }
            }
            catch {}
        }