I'm a bit lost here, so my answer may be off base. When you say you want the ExtentID
, that appears to be the name of the Dataframe you're exporting the extent of. If this is the case (which is what your code is doing to my best guess), you don't need the [x] index when printing it. :
for el in arcpy.mapping.ListLayoutElements(mxd2, "DATAFRAME_ELEMENT"):
ExtentIDStore = str(el.name)
#print x, y, z <--- You don't need to print x y and z. They are unrelated at this point.
print ExtentIDStore #used to see which item to add to the shape file
print ExtentIDStore[x] # <- This will just print an indexed character in the string
By the time you reach the loop above, your code is only concerned with the Dataframe Element in question, so there is no need to do any indexing for storage. ExtendIDStore
should be what you add to the shapefile row.
Your error which results at this line:
row.strField = str(x)
Is probably because you are not referencing a field that exists. What comes after the row reference is the name of the Field. In this case, it should be:
row.ExtentID = ExtentIDStore
File name you can get via the filePath
property of the MapDocument class and the os.path.basename
method, i.e.:
filepath = mxd2.filePath # Gets full path of MXD used in your dataframe loop
filename = os.path.basename(filepath) # Extracts the file name and extension
row.FileRef = filename
If you don't want the extension, use filename.strip(".mxd")
to remove it.
Scale would be:
scale = el.scale # Scale of the dataframe referenced in the loop
row.Scale = scale
And that should fill in the rows you're looking for.
For the new problem, I think you are misusing the insert cursor. The second argument should be a spatial reference, which cannot be gleaned via an MXD. I would recommend creating the cursor with only one argument (the output file or dfStore). You also forgot the last (and most important) line:
rows.insertRow(row)
Without that your data never gets put into the table. Put that down at the end of the row loop so that the data is actually inserted after all the variables are set.
For more information about accessing Dataframe, layer, and map properties (scale, name, current position), check out the mapping module. The scale property of the Dataframe class can be seen on the classes page of the same name.
Before making the copy of the feature class, you can open the containing workspace and check the existance of the feature class (IWorkspace2.NameExists), and if exists then delete it (IDataset.Delete).
Example:
Dim pWorkspace2 as IWorkspace2
set pWorkspace2 = yourWorkspace
' Checks the existance of the feature class
If pWorkspace2.NameExists(yourDestFeatureClass) Then
Dim pFeatureWorkspace as IFeatureWorkspace
set pFeatureWorkspace = yourWorkspace
' Open the feature class and cast it to the IDataset interface
Dim pDataset as IDataset
set pDataset = pFeatureWorkspace.OpenFeatureClass(yourDestFeatureClass)
' Removes the feature class
pDataset.Delete
End if
' Your code to make the copy...
Best Answer
You shouldn't have to delete the shapefile. Just add the following lines to your script:
That will allow
arcpy.CreateFeatureclass_management
to overwrite the existing shapefile with all of its associated files (dbf, shx, prj etc.).