CTB Files

1
Manesh Mamachan
9/22/2014 6:45 AM

Hi
How CadLib can read/write CTB files,while conveting[using PdfExporter class] to PDF.
Could you give me an example of how the CTB works? The documentation doesn't have an example.

Regards
Neethu

rammi
9/29/2014 11:10 AM

Sorry, somehow my reply I wrote for this seems to have vanished.

Reading and writing CTB is easy as could be by using the CtbFile class:

C# Code:
// Read:
CtbFile ctb = new CtbFile("input.ctb"); 
// Write:
ctb.WriteTo("output.ctb");

During rendering usage of CTB is not supported. But you could add support for some features in one of two ways.


  1. Either you prepare the DxfModel in a way that the entities reflect the CTB settings before you export it to PDF.
  2. If you are just interested in color and lineweight changes you can make use of the GrpahicsConfig.PlotStyleManager.
  3. If set you can define your own colors and lineweights for exported items. Below I'll give you example implementations of that.

How to use PlotStyle:

C# Code:
    /// <summary>
    /// Example for using different colors for some color indexes.
    /// </summary>
    public class IndexedColorExchanger {
        private readonly IDictionary<short, ArgbColor> indexToColorMapping;

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexedColorExchanger"/> class.
        /// </summary>
        /// <param name="colorMap">The color map, which only contains the colors indexes to be exchanged.</param>
        public IndexedColorExchanger(IDictionary<short, ArgbColor> colorMap) {
            indexToColorMapping = colorMap;
        }

        /// <summary>
        /// Gets the plot style.
        /// </summary>
        /// <remarks>This method can be used as a <see cref="GraphicsConfig.PlotStyleManager"/>.</remarks>
        /// <param name="plotPropertyOwner">The plot property owner (usually an entity).</param>
        /// <param name="drawContext">The draw context.</param>
        /// <returns>The plot style.</returns>
        public IPlotStyle GetPlotStyle(IPlotPropertyOwner plotPropertyOwner, DrawContext drawContext) {
            EntityColor entityColor = plotPropertyOwner.GetEntityColor(drawContext);
            if (entityColor.ColorType == ColorType.ByColorIndex) {
                ArgbColor substitutionColor;
                if (indexToColorMapping.TryGetValue(entityColor.ColorIndex, out substitutionColor)) {
                    return new OverrideColorPlotStyle(substitutionColor, plotPropertyOwner, drawContext);
                }
            }
            return new DefaultPlotStyle(plotPropertyOwner, drawContext);
        }
    }

    /// <summary>
    /// Plot manager class allowing to exchange line weights by color index.
    /// </summary>
    /// <remarks>
    /// Use <see cref="ApplyLineWeightPlotting"/> how to prepare any 
    /// <see cref="GraphicsConfig"/> to use exchanged line weights.
    /// </remarks>
    public class LineWeightChanger {
        private readonly IDictionary<short, short> indexToLineWeightMapping;

        /// <summary>
        /// Initializes a new instance of the <see cref="LineWeightChanger"/> class.
        /// </summary>
        /// <param name="indexToLineWeightMapping">
        /// The exchanged line weights.
        /// Keys are color indices, values are exchanged line weights in 100th of mm.
        /// If a color index is not present in this map, the entity's effective line weight is taken.
        /// </param>
        public LineWeightChanger(IDictionary<short, short> indexToLineWeightMapping) {
            this.indexToLineWeightMapping = indexToLineWeightMapping;
        }

        /// <summary>
        /// Gets the plot style.
        /// </summary>
        /// <remarks>This method can be used as a <see cref="GraphicsConfig.PlotStyleManager"/>.</remarks>
        /// <param name="plotPropertyOwner">The plot property owner (usually an entity).</param>
        /// <param name="drawContext">The draw context.</param>
        /// <returns>The plot style.</returns>
        public IPlotStyle GetPlotStyle(IPlotPropertyOwner plotPropertyOwner, DrawContext drawContext) {
            EntityColor entityColor = plotPropertyOwner.GetEntityColor(drawContext);
            if (entityColor.ColorType == ColorType.ByColorIndex) {
                short substitutionLineWeight;
                if (indexToLineWeightMapping.TryGetValue(entityColor.ColorIndex, out substitutionLineWeight)) {
                    switch (substitutionLineWeight) {
                        case LineWeight.ByBlock:
                            substitutionLineWeight = drawContext.ByBlockLineWeight;
                            break;
                        case LineWeight.ByLayer:
                            DxfLayer layer = plotPropertyOwner.GetLayer(drawContext);
                            substitutionLineWeight = layer.GetLineWeight(drawContext.Config);
                            break;
                        case LineWeight.Standard:
                            substitutionLineWeight = drawContext.Config.DefaultLineWeight;
                            break;
                    }
                    return new OverrideLineWeightPlotStyle(substitutionLineWeight, plotPropertyOwner, drawContext);
                }
            }
            return new DefaultPlotStyle(plotPropertyOwner, drawContext);
        }

        /// <summary>
        /// Prepare a graphics configuration which uses line weight mapping.
        /// </summary>
        /// <param name="config">The graphics configuration, unchanged.</param>
        /// <param name="indexToLineWeightMapping">Line weight mapping to use.</param>
        /// <return>Clone of <paramref name="config"/>, prepared to exchange line weights.</return>
        public static GraphicsConfig ApplyLineWeightPlotting(GraphicsConfig config, IDictionary<short, short> indexToLineWeightMapping) {
            LineWeightChanger changer = new LineWeightChanger(indexToLineWeightMapping);
            GraphicsConfig result = (GraphicsConfig)config.Clone();
            result.PlotStyleManager = changer.GetPlotStyle;
            return result;
        }
    }

Both classes can be easily adapted to use their information from the CTB file.

- Rammi

1