示例#1
0
文件: lbr_.cs 项目: khanhcegvn/TVDCEG
        public void SetNewHost(FamilyInstance familyInstance, Document doc)
        {
            using (Transaction tran = new Transaction(doc, "Unpin"))
            {
                tran.Start();
                if (familyInstance.Pinned == true)
                {
                    familyInstance.Pinned = false;
                }
                tran.Commit();
            }
            LocationPoint  locationPoint   = familyInstance.Location as LocationPoint;
            XYZ            poi             = locationPoint.Point;
            XYZ            locsau          = XYZ.Zero;
            XYZ            dri2            = familyInstance.HandOrientation;
            XYZ            lk              = familyInstance.FacingOrientation;
            var            H               = familyInstance.GetReferences(FamilyInstanceReferenceType.StrongReference);
            Transform      transformsource = familyInstance.GetTransform();
            FamilyInstance a               = null;
            var            levelnear       = GetLevelnearly(doc, familyInstance);
            var            Girdnear        = GetGridnearly(doc, familyInstance);
            var            referenealy     = GetReferencenearly(doc, familyInstance);

            if (levelnear.Count != 0)
            {
                if (referenealy.Count != 0)
                {
                    if (levelnear.Keys.First() < referenealy.Keys.First())
                    {
                        using (Transaction tran = new Transaction(doc, "cres"))
                        {
                            tran.Start();
                            FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                            MyPreProcessor         preproccessor = new MyPreProcessor();
                            options.SetClearAfterRollback(true);
                            options.SetFailuresPreprocessor(preproccessor);
                            tran.SetFailureHandlingOptions(options);
                            FamilySymbol symbol = familyInstance.Symbol;
                            var          LV     = levelnear.Values.First().GetPlaneReference();
                            a = doc.Create.NewFamilyInstance(LV, poi, dri2, symbol);
                            Mathparameter(doc, familyInstance, a);
                            tran.Commit(options);
                        }
                    }
                    else
                    {
                        using (Transaction tran = new Transaction(doc, "cres"))
                        {
                            tran.Start();
                            FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                            MyPreProcessor         preproccessor = new MyPreProcessor();
                            options.SetClearAfterRollback(true);
                            options.SetFailuresPreprocessor(preproccessor);
                            tran.SetFailureHandlingOptions(options);
                            FamilySymbol symbol = familyInstance.Symbol;
                            a = doc.Create.NewFamilyInstance(GetReferencenearly(doc, familyInstance).Values.First().GetReference(), poi, dri2, symbol);
                            Mathparameter(doc, familyInstance, a);
                            tran.Commit(options);
                        }
                    }
                }
                else
                {
                    using (Transaction tran = new Transaction(doc, "cres"))
                    {
                        tran.Start();
                        FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                        MyPreProcessor         preproccessor = new MyPreProcessor();
                        options.SetClearAfterRollback(true);
                        options.SetFailuresPreprocessor(preproccessor);
                        tran.SetFailureHandlingOptions(options);
                        FamilySymbol symbol = familyInstance.Symbol;
                        var          LV     = levelnear.Values.First().GetPlaneReference();
                        a = doc.Create.NewFamilyInstance(LV, poi, dri2, symbol);
                        Mathparameter(doc, familyInstance, a);
                        tran.Commit(options);
                    }
                }
            }
            else
            {
                if (referenealy.Count != 0)
                {
                    if (Girdnear.Count != 0)
                    {
                        if (Girdnear.Keys.First() < referenealy.Keys.First())
                        {
                            using (Transaction tran = new Transaction(doc, "cres"))
                            {
                                tran.Start();
                                FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                                MyPreProcessor         preproccessor = new MyPreProcessor();
                                options.SetClearAfterRollback(true);
                                options.SetFailuresPreprocessor(preproccessor);
                                tran.SetFailureHandlingOptions(options);
                                FamilySymbol symbol = familyInstance.Symbol;
                                var          LV     = Girdnear.Values.First().Curve.Reference;
                                a = doc.Create.NewFamilyInstance(LV, poi, dri2, symbol);
                                Mathparameter(doc, familyInstance, a);
                                tran.Commit(options);
                            }
                        }
                        else
                        {
                            using (Transaction tran = new Transaction(doc, "cres"))
                            {
                                tran.Start();
                                FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                                MyPreProcessor         preproccessor = new MyPreProcessor();
                                options.SetClearAfterRollback(true);
                                options.SetFailuresPreprocessor(preproccessor);
                                tran.SetFailureHandlingOptions(options);
                                FamilySymbol symbol = familyInstance.Symbol;
                                a = doc.Create.NewFamilyInstance(referenealy.First().Value.GetReference(), poi, dri2, symbol);
                                Mathparameter(doc, familyInstance, a);
                                tran.Commit(options);
                            }
                        }
                    }
                    else
                    {
                        using (Transaction tran = new Transaction(doc, "cres"))
                        {
                            tran.Start();
                            FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                            MyPreProcessor         preproccessor = new MyPreProcessor();
                            options.SetClearAfterRollback(true);
                            options.SetFailuresPreprocessor(preproccessor);
                            tran.SetFailureHandlingOptions(options);
                            FamilySymbol symbol = familyInstance.Symbol;
                            a = doc.Create.NewFamilyInstance(GetReferencenearly(doc, familyInstance).Values.First().GetReference(), poi, dri2, symbol);
                            Mathparameter(doc, familyInstance, a);
                            tran.Commit(options);
                        }
                    }
                }
                else
                {
                    using (Transaction tran = new Transaction(doc, "cres"))
                    {
                        tran.Start();
                        FailureHandlingOptions options       = tran.GetFailureHandlingOptions();
                        MyPreProcessor         preproccessor = new MyPreProcessor();
                        options.SetClearAfterRollback(true);
                        options.SetFailuresPreprocessor(preproccessor);
                        tran.SetFailureHandlingOptions(options);
                        FamilySymbol symbol = familyInstance.Symbol;
                        var          LV     = Girdnear.Values.First().Curve.Reference;
                        a = doc.Create.NewFamilyInstance(LV, poi, dri2, symbol);
                        Mathparameter(doc, familyInstance, a);
                        tran.Commit(options);
                    }
                }
            }
            #region Modifyelement
            Transform transformtarget = a.GetTransform();
            if (transformsource != transformtarget)
            {
                using (Transaction tr = new Transaction(doc, "flip workplane"))
                {
                    tr.Start();
                    XYZ facing = a.FacingOrientation;
                    if (facing.Equalpoint(familyInstance.FacingOrientation) == false)
                    {
                        a.FlipWorkPlane();
                    }
                    tr.Commit();
                }
            }
            using (Transaction t = new Transaction(doc, "Move element"))
            {
                t.Start();
                FailureHandlingOptions options       = t.GetFailureHandlingOptions();
                MyPreProcessor         preproccessor = new MyPreProcessor();
                options.SetClearAfterRollback(true);
                options.SetFailuresPreprocessor(preproccessor);
                t.SetFailureHandlingOptions(options);
                doc.Delete(familyInstance.Id);
                LocationPoint locp = a.Location as LocationPoint;
                XYZ           poik = locp.Point;
                if (poik.Equalpoint(poi) == false)
                {
                    XYZ vectorx = new XYZ(poik.X - poi.X, 0, 0);
                    if (vectorx.Equalpoint(XYZ.Zero) == false)
                    {
                        ElementTransformUtils.MoveElement(doc, a.Id, -vectorx);
                        XYZ    pom = (a.Location as LocationPoint).Point;
                        double kc  = Math.Round(poi.DistanceTo(pom), 0);
                        if (kc != 0)
                        {
                            ElementTransformUtils.MoveElement(doc, a.Id, vectorx * 2);
                        }
                    }
                    XYZ vectory = new XYZ(0, poik.Y - poi.Y, 0);
                    if (vectory.Equalpoint(XYZ.Zero) == false)
                    {
                        ElementTransformUtils.MoveElement(doc, a.Id, -vectory);
                        XYZ    pom = (a.Location as LocationPoint).Point;
                        double kc  = Math.Round(poi.DistanceTo(pom), 0);
                        if (kc != 0)
                        {
                            ElementTransformUtils.MoveElement(doc, a.Id, vectory * 2);
                        }
                    }
                    XYZ vectorz = new XYZ(0, 0, poik.Z - poi.Z);
                    if (vectorz.Equalpoint(XYZ.Zero) == false)
                    {
                        ElementTransformUtils.MoveElement(doc, a.Id, -vectorz);
                        XYZ    pom = (a.Location as LocationPoint).Point;
                        double kc  = Math.Round(poi.DistanceTo(pom), 0);
                        if (kc != 0)
                        {
                            ElementTransformUtils.MoveElement(doc, a.Id, vectorz * 2);
                        }
                    }
                }
                t.Commit(options);
            }
            #endregion
        }