Пример #1
0
        private void SortEntities()
        {
            ExpPen.ToolAttribute[] orderedAttributes =
            {
                ExpPen.ToolAttribute.LT_CUT,
                ExpPen.ToolAttribute.LT_CREASING,
                ExpPen.ToolAttribute.LT_PERFOCREASING,
                ExpPen.ToolAttribute.LT_HALFCUT,
                ExpPen.ToolAttribute.LT_CONSTRUCTION,
                ExpPen.ToolAttribute.LT_COTATION
            };
            // reorder entities
            List <ExpEntity> entitiesOut = new List <ExpEntity>();
            double           xLatest = _xmin, yLatest = _ymin;

            // loop through all pen types
            foreach (ExpPen.ToolAttribute attribute in orderedAttributes)
            {
                bool found = true;
                // loop through all entities
                while (found)
                {
                    found = false;
                    double    distanceToLatest = double.MaxValue;
                    ExpEntity nearEntity = null;
                    int       index = 0, nearIndex = 0;
                    foreach (ExpEntity entity in _entities)
                    {
                        // only deal with current attribute, unsorted entities
                        if (attribute != entity.Pen.Attribute || entity.Sorted)
                        {
                            continue;
                        }

                        // still some entity to be sorted
                        found = true;
                        // compute distance to latest point
                        double d = entity.Distance(xLatest, yLatest, out index);
                        if (d < distanceToLatest)
                        {
                            // save as smallest distance
                            distanceToLatest = d;
                            // save as nearest entity
                            nearEntity = entity;
                            // save as nearest point
                            nearIndex = index;
                        }
                    } // foreach
                    if (found)
                    {
                        nearEntity.Sorted = true;
                        nearEntity.Swaped = (nearIndex == 1);
                        entitiesOut.Add(nearEntity);
                        xLatest = nearEntity.X(nearIndex == 0 ? 1 : 0);
                        yLatest = nearEntity.Y(nearIndex == 0 ? 1 : 0);
                    }
                }
            }
            _entities = entitiesOut;
        }