python shp带cass属性到dwg,在CAD可通过cass进行属性查看
-- coding: utf-8 --
from pyautocad import Autocad, APoint
#from pyautocad.contrib.tables import Table
import pyautocad
import re
import win32com.client
import pythoncom
import math, time, shutil, sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QAction
from PyQt5 import QtCore, QtGui, QtWidgets
import shapefile, os, sys
import time
import sys, os
import jm
def vtInt(list):
“”“列表转化为整数”“”
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)
def vtVariant(list):
“”“列表转化为变体”“”
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)
def vtfloat(lst):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, lst)
def get_shp(path):
import shapefile
sf = shapefile.Reader(path)
shapes = sf.shapes()
#print
#print(shapes[0].parts)
#print
len(shapes) # 79条记录
# print len(list(sf.iterShapes())) #79条记录
# for name in dir(shapes[3]): #不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表
# if not name.startswith(‘__’):
# print name
#print
#print(sf.numRecords)
recds = sf.records()
# for i in range(sf.numRecords):
# rcd = sf.record(i)
#print(rcd)
# sp = rcd.shape #没有shape属性
# print (sp.points)
# recds.shape
# 读取记录
#print
#print(sf.shapeRecord(0).shape.shapeType)
#print
#print(sf.fields)
ii = 0
ywh_xh = 0
qlr_xh = 0
zddm_xh = 0
zl_xh = 0
jzmj_xh = 0
jd_pl = 10 / len(sf.fields)
jd = 0
for i in sf.fields:
#print(ii, i[0])
if i[0].lower() == 'ywh':
ywh_xh = ii - 1
if i[0].lower() == 'qlr':
qlr_xh = ii - 1
if i[0].lower() == 'zddm':
zddm_xh = ii - 1
if i[0].lower() == 'zl':
zl_xh = ii - 1
if i[0].lower() == 'zdmj':
jzmj_xh = ii -1
ii += 1
jd = jd + jd_pl
self.progressBar.setProperty("value", jd)
QApplication.processEvents()
ii = 0
#print()
#print
#print
''
ii = 0
list_ywh = {}
list_zddm = {}
list_qlr = {}
list_ywh1 = {}
list_jzmj = {}
list_zl = {}
jd_pl = 10 / len(shapes)
#jd = 10 / len(shapes)
for shp in range(len(shapes)):
ywh = sf.shapeRecord(ii).record[ywh_xh]
zddm = sf.shapeRecord(ii).record[zddm_xh]
qlr = sf.shapeRecord(ii).record[qlr_xh]
ywh = sf.shapeRecord(ii).record[ywh_xh]
jzmj = sf.shapeRecord(ii).record[jzmj_xh]
zl = sf.shapeRecord(ii).record[zl_xh]
#print(ywh)
shap = shapes[shp]
#print
#print(shap.points)
if zddm in(None, ''):
zddm = ' '
if qlr in(None, ''):
qlr = ' '
list_zddm.setdefault(ii, []).append(zddm)
list_qlr.setdefault(ii, []).append(qlr)
list_jzmj.setdefault(ii, []).append(jzmj)
list_ywh1.setdefault(ii, []).append(ywh)
list_zl.setdefault(ii, []).append(zl)
for po in shap.points:
list_ywh.setdefault(ii, []).append(po[0])
list_ywh.setdefault(ii, []).append(po[1])
list_ywh.setdefault(ii, []).append(0)
#print(list_ywh)
# #print
# print(shap.shapeType)
# #print
# len(shap.points)
# for i in range(len(shap.points)):
# #print
# print(shap.points[i])
# for x in range(len(shap.points[i])):
# #print
# print(shap.points[i][x])
ii += 1
jd = jd + jd_pl
self.progressBar.setProperty("value", jd)
QApplication.processEvents()
return list_ywh, jd, list_zddm, list_qlr, list_jzmj, list_ywh1,list_zl
def bj_hg(path):
list_ywh, jd, list_zddm, list_qlr, list_jzmj, list_ywh1,list_zl = get_shp(path)
acad = win32com.client.Dispatch(“AutoCAD.Application.19”)
filterType = vtInt([0, 8])
doc = acad.ActiveDocument
msp = doc.ModelSpace
# 删除宗地图
try:
doc.SelectionSets.Item(“SS11”).Delete()
except:
print(“Delete selection failed”)
# slt1 = doc.SelectionSets.Add(“SS11”)
# #slt1.Select(5, 0, 0, vtInt([0, 8, 1000]), vtVariant([“LWPOLYLINE”, “BDMAP_地籍”, “%s”%zddm]))
# slt1.Select(5, 0, 0, vtInt([0, 8]), vtVariant([“LWPOLYLINE”, “JZD”]))
#for item in slt1:
jd_pl = 80 / len(list_ywh)
#jd = 80 / len(list_ywh)
for i in list_ywh:
#pnts = [335769, 2744304, 0, 335779, 2744304, 0, 335779, 2744294, 0, 335769,2744294 , 0]
#print(pnt)
points = vtfloat(list_ywh[i])
plineObj = msp.AddPolyline(points)
plineObj.Closed = True
#tdyt = item.GetXData(“SOUTH”)[1][3]
DataType = [1001, 1000, 1000, 1000, 1000]
Data = [“SOUTH”, ‘300000’, list_zddm[i][0], list_qlr[i][0], ‘072’]
DataType = vtInt(DataType)
Data = vtVariant(Data)
plineObj.SetXData(DataType, Data)
DataType = [1001, 1000]
Data = ["TDZL", list_zl[i][0]]
DataType = vtInt(DataType)
Data = vtVariant(Data)
plineObj.SetXData(DataType, Data)
DataType = [1001, 1000]
Data = ["QSLYZM", list_ywh1[i][0]]
DataType = vtInt(DataType)
Data = vtVariant(Data)
plineObj.SetXData(DataType, Data)
DataType = [1001, 1000]
Data = ["JZMJ", list_jzmj[i][0]]
DataType = vtInt(DataType)
Data = vtVariant(Data)
plineObj.SetXData(DataType, Data)
jd = jd + jd_pl
self.progressBar.setProperty("value", jd)
QApplication.processEvents()
def main(self):
def askdirectory_excelcf_xz():
“”“”“”
_translate = QtCore.QCoreApplication.translate
folder = QFileDialog.getOpenFileName(caption=‘选择转换的shp’, filter=‘Txt files(*.shp)’)
self.lineEdit.setText(_translate(“Form”, folder[0]))文章来源:https://www.toymoban.com/news/detail-424071.html
def excel_cf1():
path = self.lineEdit.text()
bj_hg(path)
self.pushButton.clicked.connect(excel_cf1)
self.toolButton.clicked.connect(askdirectory_excelcf_xz)
if name == ‘main’:
app = QApplication(sys.argv)
MainWindow = QMainWindow() # QMainWindow模块名
self = jm.Ui_MainWindow()
self.setupUi(MainWindow)
main(self)文章来源地址https://www.toymoban.com/news/detail-424071.html
MainWindow.show()
sys.exit(app.exec_())
到了这里,关于python shp带cass属性到dwg,在CAD可通过cass进行属性查看的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!