예제 #1
0
        public void CreateLayerFilter()
        {
            _AcAp.Document doc = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcDb.Database db  = doc.Database;
            _AcEd.Editor   ed  = doc.Editor;

            _AcLm.LayerFilterTree       filterTree = db.LayerFilters;
            _AcLm.LayerFilterCollection filters    = filterTree.Root.NestedFilters;
            // Lets find out about the root filter
            ed.WriteMessage(String.Format("\n(Root) Name : {0} Expr : {1}", db.LayerFilters.Root.Name,
                                          db.LayerFilters.Root.FilterExpression)
                            );
            // Lets find out about all the filters
            foreach (_AcLm.LayerFilter f in filters)
            {
                ed.WriteMessage(String.Format("\nName : {0} Expr : {1}", f.Name,
                                              f.FilterExpression)
                                );
            }
            // Lets find out about the current filter
            if (db.LayerFilters.Current != null)
            {
                ed.WriteMessage(String.Format("\n(Current) Name : {0} Expr : {1}",
                                              db.LayerFilters.Current.Name,
                                              db.LayerFilters.Current.FilterExpression)
                                );
            }

            // Create and add a new layer filter
            _AcLm.LayerFilter layerFilter = new _AcLm.LayerFilter();
            layerFilter.Name             = "MyLyFilter";
            layerFilter.FilterExpression = "NAME == \"*Test*\"";
            filters.Add(layerFilter);

            // Set the changed layer filters tree to the database
            db.LayerFilters = filterTree;
        }
예제 #2
0
        private static void ImportNestedFilters(

            _AcLm.LayerFilter srcFilter,
            _AcLm.LayerFilter destFilter,
            _AcDb.Database srcDb,
            _AcDb.Database destDb,
            bool copyLayer,
            _AcEd.Editor ed)
        {
            using (_AcDb.Transaction tr = srcDb.TransactionManager.StartTransaction())
            {
                _AcDb.LayerTable lt = tr.GetObject(srcDb.LayerTableId, _AcDb.OpenMode.ForRead, false) as _AcDb.LayerTable;

                foreach (_AcLm.LayerFilter sf in srcFilter.NestedFilters)
                {
                    _AcDb.IdMapping idmap = new _AcDb.IdMapping();
                    if (copyLayer)
                    {
                        // Get the layers to be cloned to the dest db.
                        // Only those that are pass the filter
                        _AcDb.ObjectIdCollection layerIds = new _AcDb.ObjectIdCollection();
                        foreach (_AcDb.ObjectId layerId in lt)
                        {
                            _AcDb.LayerTableRecord ltr = tr.GetObject(layerId, _AcDb.OpenMode.ForRead, false) as _AcDb.LayerTableRecord;
                            if (sf.Filter(ltr))
                            {
                                layerIds.Add(layerId);
                            }
                        }

                        // clone the layers to the dest db
                        if (layerIds.Count > 0)
                        {
                            srcDb.WblockCloneObjects(
                                layerIds,
                                destDb.LayerTableId,
                                idmap,
                                _AcDb.DuplicateRecordCloning.Replace,
                                false);
                        }
                    }

                    // Find if a destination database already
                    // has a layer filter with the same name
                    _AcLm.LayerFilter df = null;
                    foreach (_AcLm.LayerFilter f in destFilter.NestedFilters)
                    {
                        if (f.Name.Equals(sf.Name))
                        {
                            df = f;
                            break;
                        }
                    }

                    if (df == null)
                    {
                        if (sf is _AcLm.LayerGroup)
                        {
                            // create a new layer filter group
                            // if nothing found
                            _AcLm.LayerGroup sfgroup = sf as _AcLm.LayerGroup;
                            _AcLm.LayerGroup dfgroup = new _AcLm.LayerGroup();
                            ed.WriteMessage(string.Format(CultureInfo.CurrentCulture, "\nImportiere Layerfilter-Gruppe {0}.", sf.Name));
                            dfgroup.Name = sf.Name;
                            df           = dfgroup;
                            if (copyLayer)
                            {
                                _AcLm.LayerCollection lyrs = sfgroup.LayerIds;
                                foreach (_AcDb.ObjectId lid in lyrs)
                                {
                                    if (idmap.Contains(lid))
                                    {
                                        _AcDb.IdPair idp = idmap[lid];

                                        dfgroup.LayerIds.Add(idp.Value);
                                    }
                                }
                            }
                            destFilter.NestedFilters.Add(df);
                        }
                        else
                        {
                            // create a new layer filter
                            // if nothing found
                            ed.WriteMessage(string.Format(CultureInfo.CurrentCulture, "\nImportiere Layerfilter {0}.", sf.Name));
                            df                  = new _AcLm.LayerFilter();
                            df.Name             = sf.Name;
                            df.FilterExpression = sf.FilterExpression;
                            destFilter.NestedFilters.Add(df);
                        }
                    }
                    // Import other filters
                    ImportNestedFilters(sf, df, srcDb, destDb, copyLayer, ed);
                }
                tr.Commit();
            }
        }