184 lines
4.9 KiB
Python
184 lines
4.9 KiB
Python
import traceback
|
|
import argparse
|
|
import os
|
|
import sys
|
|
sys.path.append('..')
|
|
import time
|
|
import threading
|
|
from netdicom.applicationentity import AE
|
|
from netdicom.SOPclass import PatientRootFindSOPClass, VerificationSOPClass
|
|
from netdicom.SOPclass import StudyRootFindSOPClass,StudyRootMoveSOPClass
|
|
import pydicom
|
|
from pydicom.dataset import Dataset
|
|
# import traceback
|
|
# import simplejson
|
|
|
|
# parse commandline
|
|
parser = argparse.ArgumentParser(description='move SCU example')
|
|
parser.add_argument('-remotehost',default='172.17.1.131')
|
|
parser.add_argument('-remoteport', type=int,default=104)
|
|
parser.add_argument('-aec', help='called AE title', default='SERVER')
|
|
parser.add_argument('-StudyInstanceUID',default='')
|
|
parser.add_argument('-dicompath',default='/home/www/rayplus_cms/app/public/DICOM')
|
|
parser.add_argument('-PatientID',default='')
|
|
parser.add_argument('-p', help='local server port', type=int, default=3032)
|
|
parser.add_argument('-aet', help='calling AE title', default='RAYPLUSAE')
|
|
|
|
args = parser.parse_args()
|
|
|
|
#
|
|
global timer
|
|
global timercount
|
|
global filepath
|
|
timercount = 55
|
|
def func_timer():
|
|
global timercount
|
|
global filepath
|
|
if timercount>-1:
|
|
if not os.path.exists(filepath):
|
|
timercount=timercount-1
|
|
timer = threading.Timer(1, func_timer)
|
|
timer.start()
|
|
else:
|
|
print "success"
|
|
os._exit(0)
|
|
|
|
else:
|
|
print "error"
|
|
os._exit(0)
|
|
|
|
|
|
# call back
|
|
def OnAssociateResponse(association):
|
|
print "Association response received"
|
|
|
|
# create application entity
|
|
MyAE = AE(args.aet, args.p, [StudyRootFindSOPClass, StudyRootMoveSOPClass,VerificationSOPClass],[])
|
|
MyAE.OnAssociateResponse = OnAssociateResponse
|
|
|
|
# remote application entity
|
|
# RemoteAE = {'Address':'localhost','Port':11112,'AET':'OsiriX'}
|
|
RemoteAE = dict(Address=args.remotehost, Port=args.remoteport, AET=args.aec)
|
|
|
|
# create association with remote AE
|
|
print "Request association"
|
|
try:
|
|
assoc = MyAE.RequestAssociation(RemoteAE)
|
|
except:
|
|
print "system error"
|
|
print traceback.print_exc()
|
|
os._exit(0)
|
|
|
|
result = []
|
|
|
|
if args.StudyInstanceUID is '' or args.PatientID is '':
|
|
print "no args"
|
|
os._exit(0)
|
|
|
|
try:
|
|
# perform a DICOM ECHO
|
|
print "DICOM Echo ... ",
|
|
st = assoc.VerificationSOPClass.SCU(1)
|
|
print 'done with status "%s"' % st
|
|
|
|
# send dataset using RTPlanStorageSOPClass
|
|
d = Dataset()
|
|
d.QueryRetrieveLevel = "IMAGE"
|
|
d.PatientName = ""
|
|
d.PatientID = args.PatientID
|
|
d.StudyID=""
|
|
|
|
d.PatientSex=""
|
|
d.PatientAge=""
|
|
d.PatientBirthDate=""
|
|
d.PatientBirthTime=""
|
|
|
|
d.StudyInstanceUID=args.StudyInstanceUID
|
|
d.SeriesInstanceUID=""
|
|
d.SOPInstanceUID=""
|
|
|
|
d.BodyPartExamined=""
|
|
d.InstitutionName=""
|
|
d.ManufacturerModelName=""
|
|
d.Manufacturer=""
|
|
d.StudyDate=""
|
|
d.StudyDescription=""
|
|
d.ModalitiesInStudy=""
|
|
d.NumberOfStudyRelatedInstances=""
|
|
d.OperatorsName=""
|
|
|
|
print "DICOM FindSCU ... "
|
|
|
|
try:
|
|
st = assoc.StudyRootFindSOPClass.SCU(d, 1)
|
|
print 'done with status "%s"' % st
|
|
|
|
print "Results"
|
|
flag = 0
|
|
for ss in st:
|
|
if not ss[1] or flag : continue
|
|
try:
|
|
if str(ss[1].StudyInstanceUID) == str(args.StudyInstanceUID):
|
|
ele = dict(StudyInstanceUID= str(ss[1].StudyInstanceUID) \
|
|
,SeriesInstanceUID= str(ss[1].SeriesInstanceUID) \
|
|
,SOPInstanceUID= str(ss[1].SOPInstanceUID) \
|
|
,StudyID= str(ss[1].StudyID) )
|
|
result.append(ele)
|
|
except:
|
|
print traceback.print_exc()
|
|
pass
|
|
except Exception:
|
|
print "Find failed"
|
|
print "error"
|
|
print traceback.print_exc()
|
|
pass
|
|
|
|
print len(result)
|
|
if len(result) >0:
|
|
try:
|
|
d = Dataset()
|
|
print
|
|
d.QueryRetrieveLevel = "IMAGE"
|
|
d.StudyInstanceUID=result[0]['StudyInstanceUID']
|
|
d.SeriesInstanceUID=result[0]['SeriesInstanceUID']
|
|
d.SOPInstanceUID=result[0]['SOPInstanceUID']
|
|
d.PatientID=args.PatientID
|
|
st = assoc.StudyRootMoveSOPClass.SCU(d, args.aet, 1)
|
|
print 'move first image done with status "%s"' % st
|
|
|
|
print "Results"
|
|
for ss in st:
|
|
print
|
|
print ss
|
|
assoc.Release(0)
|
|
except:
|
|
print "Move failed"
|
|
print "error"
|
|
print traceback.print_exc()
|
|
|
|
except:
|
|
print "Echo failed"
|
|
print traceback.print_exc()
|
|
print "error"
|
|
|
|
print "Release association"
|
|
|
|
try:
|
|
if len(result) >0:
|
|
filepath = args.dicompath+"/"+result[0]['StudyInstanceUID']+"/"+result[0]['SeriesInstanceUID']+"/"+result[0]['SOPInstanceUID']+".dcm"
|
|
timer = threading.Timer(1, func_timer)
|
|
timer.start()
|
|
except:
|
|
print traceback.print_exc()
|
|
print "Release assoc failed"
|
|
print "error"
|
|
|
|
# done
|
|
MyAE.Quit()
|
|
|
|
|
|
|
|
|
|
|
|
|