How do you obtain graphic primitives and directives from a `Graphics`

object? Leonid Shifrin showed how to remove them in the post Mathematica: Removing graphics primitives. I tried applying something similar but I can't get what I want. Consider this example:

```
g1 = ListPlot3D[
{{0, -1, 0}, {0, 1, 0}, {-1, 0, 1}, {1, 0, 1}, {-1, 1, 1}},
Mesh -> {2, 2},
Boxed -> False,
Axes -> False,
ViewPoint -> {2, -2, 1},
ViewVertical -> {0, 0, 1},
MeshStyle -> RGBColor[0, 0.5, 0],
BoundaryStyle -> RGBColor[1, 0.5, 0]
];
g2 = ImportString[ExportString[g1, "PDF", Background -> None], "PDF"][[1]]
```

`g2`

is now a graphics object. If you look at the `InputForm`

of `g2`

you will see that this graphics object is composed of `Polygon`

s and `JoinedCurve`

s. What I would like to do is able to iterate through all of the primitive objects of `g2`

. If we try to iterate as follows

```
objs = First[g2];
Table[Head[objs[[i]]], {i, 1, Length@objs}]
```

we obtain

```
{Thickness, Polygon, Polygon, Polygon, Polygon, Style, Style, Style, Style,
Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style,
Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style,
Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style,
Style, Style, Style}
```

What I would like to obtain instead is a list of simple primitives, I do not want them inside `Styles`

. Here is one attempt obtaining only the lines and colors:

```
tmp1 = Cases[objs, (_JoinedCurve | _RGBColor), Infinity];
tmp2 = DeleteCases[objs, (_Polygon | _Thickness), Infinity];
GraphicsRow[{Graphics[tmp1], Graphics[tmp2]}]
```

Notice that the image on the left is drawn incorrectly. This image was generated using only `JoinedCurve`

s and `RGBColor`

s. It somehow managed to miss one color, that is why we have a black line and then the rest of lines have the other color. The other image is drawn correctly, all we did was delete all the `Polygons`

and `Thickness`

that appeared in there. What am I doing differently here? Shouldn't we obtain the same plots?

I read:

What I would like to obtain instead is a list of simple primitives, I do not want them inside

`Styles`

.

You can get it just by simple replacement:

```
First[ g2 /. Style[expr_, opts___] :> {opts, expr} ]
```

Now you write:

Here is one attempt obtaining only the lines and colors

Knowing the internal structure of `g2`

it is simple to extract only `Line`

objects with its colors. It is even simpler because all `Line`

s are wrapped with `Style`

:

```
tmp3 = Cases[g2,
Style[{lines__Line}, ___, color_RGBColor, ___] :> {color, lines},
Infinity];
Graphics[tmp3]
```

Similar Questions

Where are some public listings of angularjs directives that people have written? FYI I'm obviously not asking about directives that are included with angularjs.

In Mathematica I have a list: x = {1,2,3,3,4,5,5,6} How will I make a list with the duplicates? Like: {3,5} I have been looking at Lists as Sets, if there is something like Except[] for lists, so I

In mathematica (I am using mma 5.0 ( guess pretty old)), if I type the following as one line: Needs[Graphics`Master`]; Animate[Plot[Sin[n x], {x, 0, 2 Pi}, Axes -> False], {n, 1, 6, 1}] I then g

What are the lowest locking primitives in linux. I know about futex_wait and futex_wake. But futexes can be used for signalling besides locking. What is the lowest locking primitive and where can I fi

I have some automatic de-serialization code that will set an object's properties using KVC. I need to add de-serialization support for primitives (int, double, float), but I am unable (or unsure of ho

I need to capture the graphics primitives from calls to java.awt.Graphics2D. The calls are made through an Open Source toolkit (Apache's PDFBox) and normally rendered in a JPanel. I would like to inte

How to inject C# Preprocessor Directives to an interface by Reflection ? Example : I want to inject #if SILVERLIGHT to any WCF service contract interface.

I need to incorporate a 3D model of the Earth into a satellite orbit intercept simulation I have created in Mathematica (I need it to work with Graphics3D[]). I have downloaded several different mode

In the AIDL guide (http://developer.android.com/guide/components/aidl.html) it mentions that all primitives are in parameters. However, I'd like to create a parameter that is a primitive and it is a

The question on making a record like in Mathematica has been discussed in few places, such as Struct data type in Mathematica?. The problem with all these methods, is that one loses the ability, it se

Does mathematica have something like select any that gets any element of a list that satisfies a criterion?

Consider a Core Data database containing Elements where each Element has a property called symbol, and the question is the most succinct method of obtaining an NSArray of each of the symbols. This can

When declaring primitives/objects, are them initialized? Which are the default value? What is the behavior on class members and local fields? What about objects declaration on class members? As answe

As I am trying to plot a few financial time series in Mathematica, I just ran into a problem illustrated in the figure below : It seems the data are no longer dealt with after Year 2000 Is there a way

I have just begin with Mathematica and am having a small problem. I have large polynomial expressions containing coefficients like A[1,1],B[2,1] which have to be integrated. The problem is that after

I have exported matrix from Mathematica Export[all.txt, all] i.e. Matrix is s.t {{1,2,3,4},{1,2,3,4}} I tried to import it back, but it is not usable.. Import[text.txt] {1, 2, 3} {1, 2, 3} , Im

So I want to be able to author an element like so: <div my-directive></div> And, I'd like my-directive to add subordinate directives. <div my-directive subordinate-directive></di

Goal: Create behaviors using directives with communication between 2 sibling elements (each their own directive). A behavior to use in example: The article content is hidden by default. When the title

I am looking to make a nice demo of the problem I mentioned in Integration in Mathematica but it is very slow yet and the Manipulate is not smooth at all. Considering the below, is there means by whic

How would you render a map such as this one within Mathematica? (e.g. one with US states and Canadian provinces colourable separately?) http://upload.wikimedia.org/wikipedia/commons/archive/1/18/20071

Mathematica can be used to write very nice documents. Does anyone know if it is possible to write documents with 2 columns? If so, can you provide some examples or links to other notebooks that show t

I am trying to solve a generalized eigenvalue problem with Mathematica. I want to find the eigenvalues and eigenvectors of the matrix A with respect to B. But when I use Eigensystem I receive the foll

I am currently attempting to run a loop in Mathematica which will attempt to insert in position {i,4} of the date list the day of the week. For some reason i can't get dayint to increase when date[[i,

I need to set an environment variable from a Mathematica notebook. Environment[VARIABLE] gives the value of the variable. But is it possible to set a variable, too?

One of Mathematica's strengths is its consistent underlying representation of objects. Thus, to change attributes of a plot without redoing the computation used to generate it, I could do something li

I am interfacing an external program with Mathematica. I am creating an input file for the external program. Its about converting geometry data from a Mathematica generated graphics into a predefined

I am trying to get a Java method to work via J/Link from Mathematica. The problem is that the method calls a JNI (.dll file). Outside of Mathematica from the command line, all works great. But calling

Is it possible to call functions from other notebooks in Mathematica, and if so how can I do it?

@using @model What else directives can be used at the top of a Razor view? In .aspx views, we have tens of directives that we can use at the top of our view, and since Razor is something new, many ar

How do I make command-line Mathematica 7 display graphics? In older versions of Mathematica, Plot[x,{x,1,2}] would popup a window displaying the plot w/ some formatting options (line thickness, equ

A template class is a multitude of possible classes, so I was asking me: the preprocessor directives #ifndef MY_CLASS #define MY_CLASS template<typename T> class My_Class {}; #endif are necessa

I'm making a small interface for calculating voltage dividers in Mathematica. I have two sliders (z1 & z2) that represent the resistor values and a couple of sliders to represent Vin as a sinusoid

What is the most simple way to share a method between 2 directives? I've tried using a factory and injecting that in my directives. But then I can't pass parameters to the factory. So I can get data b

I am trying to upgrade some equations that currently are written against Mathematica 5 to get them to work in Mathematica 7. F = Graphics[ ContourPlot[ x^2 + (2)*y^2 + (-1)*((1)/(3))*x, {x, -1, 1}, {y

What is considered to be best practice when extending the Velocity syntax. The two mechanisms available are Directives and Macros. Both look equally powerful. Directives require actual development of

How can I convert .eps to .pdf inside Mathematica (perhaps using GhostScript?)?

I need to draw primitives with graphics object with different transformation matrix. I wonder should I dispose the matrix or graphics will do it for me: using (var g = Graphics.FromImage(...)) { ... s

I'm trying to use Angular's decorator capability to add functionality to some directives. Assume that my directive's name is myDirective. My code looks like this: angular.module('app').config([ '$pr

Does anyone know how to use directives (ex. @ Import) with NHAML?

In Mathematica, how do you plot a horizontal line at a given number? How do you plot a vertical line at a given number?

I am tasked with obtaining pricing information from online tables that update periodically. Specifically, the items listed in bold indicate the items whose prices have been recently updated. I am main

Has anyone come across a tutorial to create documentation for functions you create in Mathematica? I'm trying to organize some functions that I have written but so far I'm doing a terrible job. I woul

I wanted to know, how to evaluate multiple statements in a function in Mathematica. E.g. f[x_]:=x=x+5 and then return x^2 I know this much can be modified as (x+5)^2 but originally I wanted to read d

After studying various different projects and reading as much documentation as I can handle, I've run into an issue with how to include directives in my app. The app is setup like the following: app.j

I have a question RE Retina display graphics (and potentially iPad 2 graphics if the rumours of double resolution displays are to be believed). Is there any advantage to having two graphics: Graphic.p

Mathematica can solve recursive equations using RSolve. Is it possible to have a function defined by a recurrence, regardless whether the recurrence can or cannot be solved analytically?

Can you tell me where I can see some examples (articles, opensource projects) of implementetional composite pattern on C++ (desirable full source code), on an example of problem: graphic primitives -

When do I need to initialize variables in c++? Some people assert that its important but maybe this is more an issue in c-language? I am refferering to primitives i.e. char, int, long, double Let say

Let us assume that all numbers are real. I'm trying to obtain in mathematica all values of ArcSin in an interval. In particular, the normal behavior of ArcSin in mathematica is that ArcSin[x] is in [-

I'm trying to convert an imported colored picture to greyscale. Here is what i tried so far, but mathematica simple crashes after executing this code, maybe you can find the error, can't recognize wha