public static void ModifyingXData()
        {
            Line line = new Line(Vector2.Zero, Vector2.UnitX);

            ApplicationRegistry appReg = new ApplicationRegistry("netDxf");
            XData xdata = new XData(appReg);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "Length"));
            line.XData.Add(xdata);

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(line);

            // modifying existing extended data
            line.XData[appReg.Name].XDataRecord.Add(new XDataRecord(XDataCode.Real, Vector3.Distance(line.StartPoint, line.EndPoint)));

            // adding new extended data entry to an existing entity
            ApplicationRegistry appReg2 = new ApplicationRegistry("newXData");
            XData xdata2 = new XData(appReg2);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "XData entries"));
            line.XData.Add(xdata2);

            Debug.Assert(ReferenceEquals(line.XData[appReg2.Name].ApplicationRegistry, doc.ApplicationRegistries[appReg2.Name]));

            // deleting existing extended data
            line.XData.Remove(appReg.Name);

            // we can also change the name of the application registry name
            doc.ApplicationRegistries["newXData"].Name = "netDxfRenamed";

            doc.Save("xData.dxf");

            doc = DxfDocument.Load("xData.dxf");
        }
Beispiel #2
0
        protected virtual void OnXDataRemoveAppRegEvent(ApplicationRegistry item)
        {
            XDataRemoveAppRegEventHandler ae = this.XDataRemoveAppReg;

            if (ae != null)
            {
                ae(this, new ObservableCollectionEventArgs <ApplicationRegistry>(item));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new ApplicationRegistry that is a copy of the current instance.
        /// </summary>
        /// <param name="newName">ApplicationRegistry name of the copy.</param>
        /// <returns>A new ApplicationRegistry that is a copy of this instance.</returns>
        public override TableObject Clone(string newName)
        {
            ApplicationRegistry copy = new ApplicationRegistry(newName);

            foreach (XData data in this.XData.Values)
            {
                copy.XData.Add((XData)data.Clone());
            }

            return(copy);
        }
Beispiel #4
0
 /// <summary>
 /// Initialize a new instance of the <c>XData</c> class .
 /// </summary>
 /// <param name="appReg">Name of the application associated with the list of extended data records.</param>
 public XData(ApplicationRegistry appReg)
 {
     this.appReg = appReg;
     this.xData = new List<XDataRecord>();
 }
 protected virtual void OnXDataRemoveAppRegEvent(ApplicationRegistry item)
 {
     XDataRemoveAppRegEventHandler ae = this.XDataRemoveAppReg;
     if (ae != null)
         ae(this, new ObservableCollectionEventArgs<ApplicationRegistry>(item));
 }
        /// <summary>
        /// Writes a new extended data application registry to the table section.
        /// </summary>
        /// <param name="appReg">Name of the application registry.</param>
        private void WriteApplicationRegistry(ApplicationRegistry appReg)
        {
            Debug.Assert(this.activeTable == DxfObjectCode.ApplicationIDTable);

            this.chunk.Write(0, DxfObjectCode.ApplicationIDTable);
            this.chunk.Write(5, appReg.Handle);
            this.chunk.Write(330, appReg.Owner.Handle);

            this.chunk.Write(100, SubclassMarker.TableRecord);
            this.chunk.Write(100, SubclassMarker.ApplicationId);

            this.chunk.Write(2, this.EncodeNonAsciiCharacters(appReg.Name));

            this.chunk.Write(70, (short) 0);
        }
 /// <summary>
 /// Determines whether the specified <see cref="netDxf.Tables.ApplicationRegistry" /> is equal to the current <see cref="netDxf.Tables.ApplicationRegistry" />.
 /// </summary>
 /// <returns>
 /// True if the specified <see cref="netDxf.Tables.ApplicationRegistry" /> is equal to the current <see cref="netDxf.Tables.ApplicationRegistry" />; otherwise, false.
 /// </returns>
 /// <remarks>Two <see cref="netDxf.Tables.ApplicationRegistry" /> instances are equal if their names are equal.</remarks>
 /// <exception cref="T:System.NullReferenceException">
 /// The <paramref name="obj" /> parameter is null.
 /// </exception>
 public bool Equals(ApplicationRegistry obj)
 {
     return(obj.name == this.name);
 }
Beispiel #8
0
        /// <summary>
        /// Writes a new extended data application registry to the table section.
        /// </summary>
        /// <param name="appReg">Nombre del registro de aplicación.</param>
        public void RegisterApplication(ApplicationRegistry appReg)
        {
            if (this.activeTable != StringCode.ApplicationIDTable)
            {
                throw new InvalidDxfTableException(StringCode.ApplicationIDTable, this.file);
            }

            this.WriteCodePair(0, StringCode.ApplicationIDTable);
            this.WriteCodePair(5, appReg.Handle);
            this.WriteCodePair(100, SubclassMarker.TableRecord);
            this.WriteCodePair(100, SubclassMarker.ApplicationId);
            this.WriteCodePair(2, appReg);
            this.WriteCodePair(70, 0);
        }
Beispiel #9
0
        private XData ReadXDataRecord(ApplicationRegistry appReg)
        {
            XData xData = new XData(appReg);
            this.chunk.Next();

            while (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
            {
                if (this.chunk.Code == (short) XDataCode.AppReg)
                    break;

                XDataCode code = (XDataCode) this.chunk.Code;
                object value = null;
                switch (code)
                {
                    case XDataCode.String:
                        value = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        break;
                    case XDataCode.AppReg:
                        // Application name cannot appear inside the extended data, AutoCAD assumes it is the beginning of a new application extended data group
                        break;
                    case XDataCode.ControlString:
                        value = this.chunk.ReadString();
                        break;
                    case XDataCode.LayerName:
                        value = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        break;
                    case XDataCode.BinaryData:
                        value = this.chunk.ReadBytes();
                        break;
                    case XDataCode.DatabaseHandle:
                        value = this.chunk.ReadString();
                        break;
                    case XDataCode.RealX:
                    case XDataCode.RealY:
                    case XDataCode.RealZ:
                    case XDataCode.WorldSpacePositionX:
                    case XDataCode.WorldSpacePositionY:
                    case XDataCode.WorldSpacePositionZ:
                    case XDataCode.WorldSpaceDisplacementX:
                    case XDataCode.WorldSpaceDisplacementY:
                    case XDataCode.WorldSpaceDisplacementZ:
                    case XDataCode.WorldDirectionX:
                    case XDataCode.WorldDirectionY:
                    case XDataCode.WorldDirectionZ:
                    case XDataCode.Real:
                    case XDataCode.Distance:
                    case XDataCode.ScaleFactor:
                        value = this.chunk.ReadDouble();
                        break;
                    case XDataCode.Int16:
                        value = this.chunk.ReadShort();
                        break;
                    case XDataCode.Int32:
                        value = this.chunk.ReadInt();
                        break;
                }

                XDataRecord xDataRecord = new XDataRecord(code, value);
                xData.XDataRecord.Add(xDataRecord);
                this.chunk.Next();
            }

            return xData;
        }
 /// <summary>
 /// Determines whether the specified <see cref="netDxf.Tables.ApplicationRegistry" /> is equal to the current <see cref="netDxf.Tables.ApplicationRegistry" />.
 /// </summary>
 /// <returns>
 /// True if the specified <see cref="netDxf.Tables.ApplicationRegistry" /> is equal to the current <see cref="netDxf.Tables.ApplicationRegistry" />; otherwise, false.
 /// </returns>
 /// <remarks>Two <see cref="netDxf.Tables.ApplicationRegistry" /> instances are equal if their names are equal.</remarks>
 /// <exception cref="T:System.NullReferenceException">
 /// The <paramref name="obj" /> parameter is null.
 /// </exception>
 public bool Equals(ApplicationRegistry obj)
 {
     return obj.name == this.name;
 }
        private static void AppRegUsesAndRemove()
        {
            DxfDocument dxf = new DxfDocument();

            List<PolylineVertex> vertexes = new List<PolylineVertex>{
                                                                        new PolylineVertex(0, 0, 0), 
                                                                        new PolylineVertex(10, 0, 10), 
                                                                        new PolylineVertex(10, 10, 20), 
                                                                        new PolylineVertex(0, 10, 30)
                                                                        };

            Polyline poly = new Polyline(vertexes, true);

            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));

            poly.XData.Add(xdata1);

            dxf.AddEntity(poly);

            Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5));

            ApplicationRegistry myAppReg = new ApplicationRegistry("MyAppReg");
            XData xdata2 = new XData(myAppReg);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Distance, Vector3.Distance(line.StartPoint, line.EndPoint)));
            line.XData.Add(xdata2);

            dxf.AddEntity(line);

            Circle circle = new Circle(Vector3.Zero, 15);
            XData xdata3 = new XData(myAppReg);
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.Real, circle.Radius));
            circle.XData.Add(xdata3);

            dxf.AddEntity(circle);

            dxf.Save("appreg.dxf");

            DxfDocument dxf2 = DxfDocument.Load("appreg.dxf");

            // will return false the "MyAppReg" is in use
            bool ok;
            ok = dxf.ApplicationRegistries.Remove(myAppReg.Name);
            dxf.RemoveEntity(line);
            dxf.RemoveEntity(circle);
            // "MyAppReg" is not used anymore
            IList<DxfObject> uses = dxf.ApplicationRegistries.GetReferences(myAppReg.Name);
            // it is safe to delete it
            ok = dxf.ApplicationRegistries.Remove(myAppReg.Name);
            
            // we can even make a full cleanup
            dxf.ApplicationRegistries.Clear();

            dxf.Save("appreg2.dxf");


        }
        private static void BinaryChunkXData()
        {
            Line line = new Line(Vector2.Zero, Vector2.UnitX);

            ApplicationRegistry appId = new ApplicationRegistry("TestBinaryChunk");

            // the extended data binary data (code 1004) is stored in a different way depending if the dxf file is text or binary.
            // in text based files as a string of hexadecimal digits, two per binary byte,
            // while in binary files the data is stored in chunks of 127 bytes, preceding a byte that defines the number of bytes in the chunk
            byte[] data = new byte[325];

            // fill up the array with some random data
            Random rnd = new Random();
            rnd.NextBytes(data);

            XData xdata = new XData(appId);

            // the XDataRecord will store the binary data as a byte array and not as a string as it use to be
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.BinaryData, data));
            line.XData.Add(xdata);

            DxfDocument dxf = new DxfDocument();
            dxf.AddEntity(line);

            dxf.Save("BinaryChunkXData.dxf");
            dxf.Save("BinaryChunkXData binary.dxf", true);

            // some testing
            DxfDocument test = DxfDocument.Load("BinaryChunkXData binary.dxf");
            Line lineTest = test.Lines[0];
            XDataRecord recordTest = lineTest.XData[appId.Name].XDataRecord[0];
            Debug.Assert(recordTest.Code == XDataCode.BinaryData);
            byte[] dataText = (byte[]) recordTest.Value;

            byte[] compare = new byte[127];
            Array.Copy(data, compare, 127);

            for (int i = 0; i < 127; i++)
            {
                Console.WriteLine(dataText[i] == compare[i]);
            }

            // this is the string as it is saved in text based dxf files
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dataText.Length; i++)
            {
                sb.Append(String.Format("{0:X2}", data[i]));
            }
            Console.WriteLine(sb.ToString());

            Console.WriteLine();
            Console.WriteLine("Press a key to continue...");
            Console.ReadKey();
        }
 static ApplicationRegistry()
 {
     defaultApp = new ApplicationRegistry("ACAD");
 }