Esempio n. 1
0
        internal int PrepareBeforeWrite()
        {
            // Remove must-not elements
            m_Linetypes.Remove("ByBlock");
            m_Linetypes.Remove("ByLayer");
            m_Linetypes.Remove("Continuous");

            // Add must-have elements
            if (!m_Layers.ContainsKey("0"))
            {
                m_Layers["0"] = new LayerData();
            }

            if (!m_TextStyles.ContainsKey("Standard"))
            {
                m_TextStyles["Standard"] = new TextStyleData();
            }

            if (!m_DimStyles.ContainsKey("ISO-25"))
            {
                m_DimStyles["ISO-25"] = new DimStyleData(0);
            }

            if (!m_MLeaderStyles.ContainsKey("Standard"))
            {
                m_MLeaderStyles["Standard"] = new MLeaderStyle();
            }

            if (!m_MLeaderStyles.ContainsKey("Annotative"))
            {
                m_MLeaderStyles["Annotative"] = new MLeaderStyle();
                m_MLeaderStyles["Annotative"].m_IsAnnotative = true;
            }

            if (!m_TableStyles.ContainsKey("Standard"))
            {
                m_TableStyles["Standard"] = new TableStyle();
            }

            // Generate blocks for dimensions
            string blockName = null;
            GenerateBlockNameFn GenerateBlockName = (int blockNo) =>
            {
                blockName = String.Format("*D{0}", blockNo);
            };

            foreach (var ent in m_Objects)
            {
                AcadDim pDim = ent as AcadDim;
                if (pDim != null && String.IsNullOrEmpty(pDim.m_BlockName))
                {
                    GenerateBlockName(++m_TmpBlockNo);
                    pDim.GenerateBlock(this, blockName);
                }
            }

            foreach (var pair in m_RealBlockDefs)
            {
                foreach (var ent in pair.Value.m_Objects)
                {
                    AcadDim pDim = ent as AcadDim;
                    if (pDim != null && String.IsNullOrEmpty(pDim.m_BlockName))
                    {
                        GenerateBlockName(++m_TmpBlockNo);
                        pDim.GenerateBlock(this, blockName);
                    }
                }
            }

            foreach (var ld in m_Layouts)
            {
                foreach (var ent in ld.Value.m_Objects)
                {
                    AcadDim pDim = ent as AcadDim;
                    if (pDim != null && String.IsNullOrEmpty(pDim.m_BlockName))
                    {
                        GenerateBlockName(++m_TmpBlockNo);
                        pDim.GenerateBlock(this, blockName);
                    }
                }
            }

            // Assign handle to entities. Start from 10000, it should be enough to hold other handles.
            int handle = 10000;

            // It is harmless to assign handle to entities even if it is not exported to DXF.
            foreach (var pb in DxfWriter.s_PredefinedBlocks)
            {
                foreach (var ent in pb)
                {
                    ent.AssignHandle(ref handle);
                }
            }

            foreach (var ent in m_Objects)
            {
                ent.AssignHandle(ref handle);
            }

            foreach (var pair in m_RealBlockDefs)
            {
                foreach (var ent in pair.Value.m_Objects)
                {
                    ent.AssignHandle(ref handle);
                }
            }

            // Add must-have layout
            if (m_Layouts.Count == 0)
            {
                var pLayout = new LayoutData();
                m_Layouts["Layout1"] = pLayout;
            }

            int i = -1;

            foreach (var ld in m_Layouts)
            {
                // Assign block name to layouts
                if (i == -1)
                {
                    ld.Value.m_BlockName = "*Paper_Space";
                }
                else
                {
                    ld.Value.m_BlockName = String.Format("*Paper_Space{0}", i);
                }
                ld.Value.m_LayoutOrder = i + 2;
                ++i;
                foreach (var ent in ld.Value.m_Objects)
                {
                    ent.AssignHandle(ref handle);
                }
            }

            return(handle);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            String curfile      = System.Reflection.Assembly.GetExecutingAssembly().Location;
            String curDirectory = Path.GetDirectoryName(curfile);

            string text  = "一二三四五六七八九二二三四五六七八九三二三四五六七八九四二三四五六七八九五二三四五六七八九六二三四五六七八九七二三四五六七八九八二三四五六七八九九二三四五六七八九十二三四五六七八九";
            int    len   = text.Length;
            int    start = 0;
            int    cur   = start;

            while (true)
            {
                cur = Global.AdvanceUtf8(text, start, 250);
                if (cur == len || cur == -1)
                {
                    Console.WriteLine(text.Substring(start, cur - start));
                    break;
                }
                Console.WriteLine(text.Substring(start, cur - start));
                start = cur;
            }

            // Test DXF read/write
            Global.EnableDebugInfo(true);
            DxfData dxf  = new DxfData();
            var     pDim = new AcadDimAln();

            pDim.m_DefPoint.SetPoint(80, 120);
            pDim.m_TextPosition = new CDblPoint(30, 70);
            pDim.m_ExtLine1Point.SetPoint(0, 0);
            pDim.m_ExtLine2Point.SetPoint(100, 100);
            pDim.m_DimStyleName = "ISO-25";
            dxf.m_Objects.Add(pDim);
            //auto pTable = std::make_shared<AcadTable>();
            //pTable->m_InsertionPoint.SetPoint(0, 0);
            //pTable->InitializeCells(2, 2, DXF::CellType::TEXT_CELL);
            //pTable->SetColWidth(600);
            //pTable->SetRowHeight(330);
            //for (int i = 0; i < pTable->m_RowCount; ++i)
            //    for (int j = 0; j < pTable->m_ColCount; ++j)
            //    {
            //        Cell & c = pTable->GetCell(i, j);
            //        c.m_TextHeight = 250;
            //        std::shared_ptr<CellText> & ct = dynamic_pointer_cast<CellText>(c.m_Content);
            //        ct->m_Text = "0*0";
            //    }
            //dxf.m_Objects.push_back(pTable);

            //====================== 文字样式 ======================
            TextStyleData txtStyle = new TextStyleData();

            //
            txtStyle.m_TrueType = "宋体";  //SimSun
            //
            txtStyle.m_PrimaryFontFile = "fsdb_e.shx";
            txtStyle.m_BigFontFile     = "hztxt.shx";
            //
            txtStyle.m_ObliqueAngle = 20;
            txtStyle.m_WidthFactor  = 0.7;
            txtStyle.m_Height       = 2.5;
            //
            dxf.m_TextStyles.Add("字体1", txtStyle);


            //====================== 标注样式 ======================
            DimStyleData dimStyle = new DimStyleData();

            dimStyle.TextStyle            = "字体1";
            dimStyle.VerticalTextPosition = AcDimVerticalJustification.acAbove;
            //
            dxf.m_DimStyles.Add("标注1", dimStyle);

            AcadDimRot dim = new AcadDimRot();

            dim.m_DefPoint.SetPoint(80, 120);
            dim.m_TextPosition = new CDblPoint(30, 70);
            dim.m_ExtLine1Point.SetPoint(0, 0);
            dim.m_ExtLine2Point.SetPoint(100, 100);
            dim.m_DimStyleName = "标注1";
            dxf.m_Objects.Add(dim);


            //====================== 填充 ======================
            var pH = new AcadHatch();
            var pC = new AcadCircle();

            pC.m_Center.SetPoint(10, 10);
            pC.m_Radius = 50;
            pH.AddEntity(pC, -1);
            pH.AddAssociatedEntity(pC, -1);
            pH.m_PatternName = "SOLID";
            pC.m_Reactors.Add(new WeakReference(pH));
            dxf.m_Objects.Add(pC);
            dxf.m_Objects.Add(pH);

            //====================== 线宽 ======================
            var pP2a = new AcadLWPLine();

            pP2a.m_Vertices.Add(new CDblPoint(20, 0));
            pP2a.m_Vertices.Add(new CDblPoint(30, 0));
            //pP2a.SetStartWidth(0, 0);
            //pP2a.SetEndWidth(0, 10);
            pP2a.SetWidth(0, 0, 10);
            // pP2a.SetWidth(1, 10, 10);
            dxf.m_Objects.Add(pP2a);

            var pP2b = new AcadLWPLine();

            pP2b.m_Vertices.Add(new CDblPoint(20, 20));
            pP2b.m_Vertices.Add(new CDblPoint(30, 20));
            pP2b.SetConstWidth(10);
            dxf.m_Objects.Add(pP2b);

            //====================== 填充 ======================
            var pP3 = new AcadLWPLine();

            pP3.m_Vertices.Add(new CDblPoint(30, 30));
            pP3.m_Vertices.Add(new CDblPoint(40, 40));
            pP3.m_Vertices.Add(new CDblPoint(40, 20));
            pP3.m_Closed = true;

            var pH3 = new AcadHatch();

            pH3.AddEntity(pP3, -1);
            pH3.AddAssociatedEntity(pP3, -1);
            pH3.m_PatternName = "SOLID";
            pP3.m_Reactors.Add(new WeakReference(pH3));
            dxf.m_Objects.Add(pP3);
            dxf.m_Objects.Add(pH3);


            DxfWriter dxfWriter = new DxfWriter(dxf);

            //dxfWriter.WriteDxf(@"G:\SharedWithVM\test2.dxf",  @"..\DxfCs\acadiso.lin", @"..\DxfCs\acadiso.pat", new CDblPoint(0, 0), 100);
            dxfWriter.WriteDxf(curDirectory + @"\test2.dxf", curDirectory + @"\acadiso.lin", curDirectory + @"\acadiso.pat", new CDblPoint(0, 0), 100);
            if (args.Length == 1)
            {
                DxfReader dxfReader = new DxfReader(dxf);
                if (dxfReader.ReadDxf(args[0], false))
                {
                    dxfWriter.WriteDxf(@"G:\SharedWithVM\test3.dxf", @"..\DXF\acadiso.lin", @"..\DXF\acadiso.pat", new CDblPoint(2000, 1400), 3300);
                }
            }
        }